Microsoft Small Basic

Program Listing: MMN231
' キーの押し下げ状態を表示するサンプルプログラムのつもり
' カーソルキーと、Z、Xキーの状態を表示する

' Z、XキーをA、Bボタンとして扱う
button_a_code = "Z"
button_b_code = "X"

' キーの押し下げ状態を記憶しておく変数を初期化
keyinfo["Right"] = "False"
keyinfo["Left"] = "False"
keyinfo["Down"] = "False"
keyinfo["Up"] = "False"
keyinfo["Button_a"] = "False"
keyinfo["Button_b"] = "False"
keyinfo["Esc"] = "False"

' グラフィックウインドウを初期化
GraphicsWindow.Width = 320
GraphicsWindow.Height = 240
GraphicsWindow.BackgroundColor = "White"
GraphicsWindow.BrushColor = "Black"

' キーを押した時、離した時に呼ばれるサブルーチンを設定
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

counter = 0
keyvalue = 0

' メインループ
While ("True")

' Escキーが押されていたらプログラムを終了
If (keyinfo["Esc"] = "True") Then
Goto ProgramEnd
EndIf

counter = counter + 1

GraphicsWindow.Clear()
DrawKeyInfo()
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(0,180, "押されたキー: "+keyvalue)
GraphicsWindow.DrawText(0,200, "ESCキーで終了.")
GraphicsWindow.DrawText(0,220,"カウンター:"+counter)

' 時間待ち
Program.Delay(16)
EndWhile

ProgramEnd:
Program.End()

'----------------------------------------
' キーが押された時
Sub OnKeyDown
key = GraphicsWindow.LastKey
keyvalue = key
If ( key = "Escape" ) Then
keyinfo["Esc"] = "True"
EndIf
If ( key = "Right" ) Then
keyinfo["Right"] = "True"
EndIf
If ( key = "Left" ) Then
keyinfo["Left"] = "True"
EndIf
If ( key = "Down" ) Then
keyinfo["Down"] = "True"
EndIf
If ( key = "Up" ) Then
keyinfo["Up"] = "True"
EndIf
If ( key = button_a_code ) Then
keyinfo["Button_a"] = "True"
EndIf
If ( key = button_b_code ) Then
keyinfo["Button_b"] = "True"
EndIf
EndSub

'----------------------------------------
'キーが離された時
Sub OnKeyUp
key = GraphicsWindow.LastKey
If ( key = "Escape" ) Then
keyinfo["Esc"] = "False"
EndIf
If ( key = "Right" ) Then
keyinfo["Right"] = "False"
EndIf
If ( key = "Left" ) Then
keyinfo["Left"] = "False"
EndIf
If ( key = "Down" ) Then
keyinfo["Down"] = "False"
EndIf
If ( key = "Up" ) Then
keyinfo["Up"] = "False"
EndIf
If ( key = button_a_code ) Then
keyinfo["Button_a"] = "False"
EndIf
If ( key = button_b_code ) Then
keyinfo["Button_b"] = "False"
EndIf
EndSub

'----------------------------------------
' キーの押し下げ状態を画面に描画
Sub DrawKeyInfo
If (keyinfo["Right"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(30,20,10,10)

If (keyinfo["Left"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(10,20,10,10)

If (keyinfo["Up"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(20,10,10,10)

If (keyinfo["Down"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(20,30,10,10)

If (keyinfo["Button_a"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(45,25,10,10)

If (keyinfo["Button_b"] = "True") Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Gray"
EndIf
GraphicsWindow.FillRectangle(60,25,10,10)

GraphicsWindow.BrushColor = "Black"
EndSub