Пример DirectInput

Пример, показывающий работу с мышью и клавиатурой посредством интерфейса DirectInput

Платформа: Windows
Автор: Станислав Будинов

#INCLUDE "windows.bi"
#INCLIB "dinput8"
#INCLIB "dxguid"
#INCLUDE "win/dinput.bi"


Sub BDIERR(a As HRESULT, s As String, f As BOOL=0)
    If FAILED(a) And f=1 Then
        ? s
        Sleep
        End
    Elseif FAILED(a) Then
        ? s
    Endif
End Sub

Dim As hwnd hwnd
Dim As IDirectInput8 Ptr g_pDI
Dim As IDirectInputDevice8A Ptr DI_Mouse,DI_KeyBoard
Dim MouseState As DIMOUSESTATE2
Dim dipdw(256) As Ubyte

Screen 17
Screencontrol(2,Cint(hwnd))

BDIERR(DirectInput8Create(GetModuleHandle(Null), DIRECTINPUT_VERSION,_
@IID_IDirectInput8, @g_pDI, NULL),"failed DirectInput8Create")

BDIERR(g_pDI->lpVtbl->CreateDevice(g_pDI,@GUID_SysMouse, @DI_Mouse, Null),_
"failed CreateDevice mouse",1)
BDIERR(g_pDI->lpVtbl->CreateDevice(g_pDI,@GUID_SysKeyboard, @DI_KeyBoard, Null),_
"failed CreateDevice keyboard",1)

BDIERR(DI_Mouse->lpVtbl->SetDataFormat(DI_Mouse,@c_dfDIMouse2),_
"failed SetDataFormat mouse",1)
BDIERR(DI_KeyBoard->lpVtbl->SetDataFormat(DI_KeyBoard,@c_dfDIKeyboard),_
"failed SetDataFormat keyboard",1)

BDIERR(DI_Mouse->lpVtbl->SetCooperativeLevel(DI_Mouse,hwnd, DISCL_FOREGROUND+DISCL_NONEXCLUSIVE),_
"failed SetCooperativeLevel mouse",1)
BDIERR(DI_KeyBoard->lpVtbl->SetCooperativeLevel(DI_KeyBoard,hwnd, DISCL_FOREGROUND+DISCL_NONEXCLUSIVE),_
"failed SetCooperativeLevel keyboard",1)

Do
    BDIERR(DI_Mouse->lpVtbl->Acquire(DI_Mouse),"failed Acquire mouse")
    BDIERR(DI_KeyBoard->lpVtbl->Acquire(DI_KeyBoard),"failed Acquire keyboard")
    DI_Mouse->lpVtbl->GetDeviceState(DI_Mouse,Sizeof(DIMOUSESTATE2), @MouseState )
    DI_KeyBoard->lpVtbl->GetDeviceState(DI_KeyBoard,256, @dipdw(0) )
    ? MouseState.lx,MouseState.ly,MouseState.rgbButtons(0),_
    MouseState.rgbButtons(1),MouseState.rgbButtons(2)
    For I As Integer =0 To 255
        If dipdw(i)<>0 Then
            ? Chr(MapVirtualKeyEx(i, 1, 0))
            If i =1 Then Exit Do
        Endif
    Next    
    Sleep(10)
Loop 

DI_Mouse->lpVtbl->Release(DI_Mouse)
DI_KeyBoard->lpVtbl->Release(DI_KeyBoard)
g_pDI->lpVtbl->Release(g_pDI)