キー入力

キー入力を検知する

今回は、マルチスレッドを利用した例として、キー入力検知をしてみます。
とりあえず、プログラムです。[ESC]で終了します。
#console
#include<vcrt71.sbp>

Dim hThre As HANDLE
Dim ID As *DWord
Dim code As DWord

'キーが押されていれば0以外、押されていなければ0を返す
Function Inp(key As Long) As Long
    Inp = GetAsyncKeyState(key) and &H8000
End Function

'スレッド関数
Function Thre (lp As DWord) As DWord
    'スレッドの処理をここに書きます。
    printf(Ex"[ESC]で終了します\n")
    While Inp(VK_ESCAPE) = 0
        If Inp(VK_UP) Then printf(Ex"↑\n")
        If Inp(VK_DOWN) Then printf(Ex"↓\n")
        If Inp(VK_RIGHT) Then printf(Ex"→\n")
        If Inp(VK_LEFT) Then printf(Ex"←\n")
        Sleep(50)
    Wend

End Function

'スレッドを作る
hThre = CreateThread(ByVal NULL , 0 , AddressOf(Thre) , 0 , 0 , ID)

'スレッド終了まで待つ
GetExitCodeThread(hThre , code)
While code = STILL_ACTIVE
    Sleep(1)
    GetExitCodeThread(hThre , code)
Wend
これは、GetAsyncKeyState関数を使って、キーの状態をしらべています。
キーが押されていれば16ビット整数で最上位ビットが1になって戻ります。
Inp関数で、最上位ビット以外を0にして戻しています。
別にInp関数を作る必要はありません。本家の講座でもこんなことはしていません。

絵を動かす

今度は絵を動かしてみます。上のプログラムとほとんど変わりませんが。
#console
#include<vcrt71.sbp>

Dim hThre As HANDLE
Dim ID As *DWord
Dim code As DWord

'キーが押されていれば0以外、押されていなければ0を返す
Function Inp(key As Long) As Long
    Inp = GetAsyncKeyState(key) and &H8000
End Function

'スレッド関数
Function Thre (lp As DWord) As DWord
    'スレッドの処理をここに書きます。
    Dim hWnd As HWND
    Dim hDC As HDC
    Dim x As Long,y As Long
    hWnd = GetForegroundWindow()
    hDC = GetDC(hWnd)
    SetBkColor(hDC , RGB(0,0,0))
    SetTextColor(hDC , RGB(200,255,100))
    x = 0
    y = 0
    While Inp(VK_ESCAPE) = 0
        If Inp(VK_UP) Then
            TextOut(hDC , x*16 , y*16 , " " , 2)
            y=y-1
            If y<0  Then y= 0 
        Else If Inp(VK_DOWN) Then
            TextOut(hDC , x*16 , y*16 , " " , 2)
            y=y+1
            If y>20 Then y=20 
        Else If Inp(VK_RIGHT) Then
            TextOut(hDC , x*16 , y*16 , " " , 2)
            x=x+1
            If x>20  Then x=20
        Else If Inp(VK_LEFT) Then
            TextOut(hDC , x*16 , y*16 , " " , 2)
            x=x-1
            If x<0 Then x=0
        End If
        TextOut(hDC , x*16 , y*16 , "●" , 2)
        Sleep(50)
    Wend
    ReleaseDC(hWnd , hDC)
End Function

'スレッドを作る
hThre = CreateThread(ByVal NULL , 0 , AddressOf(Thre) , 0 , 0 , ID)

'スレッド終了まで待つ
GetExitCodeThread(hThre , code)
While code = STILL_ACTIVE
    Sleep(1)
    GetExitCodeThread(hThre , code)
Wend