I have two programs that needed raw input from a hid whether the program was in the foreground or not. The first needed to see a wireless presenter trigger only and the other needed to see specific keys. The three key examples are just some of the ways that I needed to deal with the input.
Code:
'specific buttons on a (kensington) presenter hid %Hid_Left = 33 '%VK_PGUP %Hid_Right = 34 '%VK_PGDN %Hid_Bottom = 66 '%VK_B Global capturehid As Long 'flag used to capture hid handle '-------------------------------------------------------------------------------- Function FRMMAIN_WM_CREATE (hWndForm As Dword, ByVal UserData As Long) As Long Local rdi As RID_DEVICE_INFO Dim ridl(1) As RAWINPUTDEVICELIST GetRawInputDeviceList(ByVal %Null, nDevices&, SizeOf(ridl(0))) 'get hid count ReDim ridl(nDevices&) 'hid array GetRawInputDeviceList(ridl(0), nDevices&, SizeOf(ridl(0))) 'get hids Dim rid(nDevices&) As RAWINPUTDEVICE 'hid register array i& = 0 'keyboard counter For d& = 0 To nDevices& - 1 'hid loop GetRawInputDeviceInfo(ridl(d&).hDevice, %RIDI_DEVICEINFO, rdi, SizeOf(rdi)) 'get hid info Select Case rdi.dwtype 'poll hid type Case %RIM_TYPEMOUSE 'mouse Case %RIM_TYPEKEYBOARD 'keyboard (the presenter shows up as a keyboard too) rid(i&).usUsagePage = 1 rid(i&).usUsage = 6 rid(i&).dwFlags = %RIDEV_EXINPUTSINK 'capture input always rid(i&).hwndTarget = hWndForm Incr i& Case %RIM_TYPEHID 'other hid End Select RegisterRawInputDevices(rid(0), i&, SizeOf(rid(0))) 'register hids Next End Function '-------------------------------------------------------------------------------- Function FRMMAIN_CUSTOM (hWndForm As Dword, wMsg As Long, wParam As Dword, lParam As Long) As Long Static hidDevice As Dword Static hidF9 As Long Select Case wMsg Case %WM_INPUT GetRawInputData(lParam, %RID_INPUT, ByVal %Null, dwSize&, SizeOf(RAWINPUTHEADER)) 'get size of hid input lpb$ = Space$(dwSize&) 'set string for hid input GetRawInputData(lParam, %RID_INPUT, ByVal StrPtr(lpb$), dwSize&, SizeOf(RAWINPUTHEADER)) 'get hid input Dim raw As RAWINPUT LSet raw = lpb$ 'convert input string to rawinput 'if you only want to see a specific hid then set the capture flag and push a key on the hid to capture the hid handle If capturehid = %True Then If (raw.keyboard.message And %WM_KEYUP) = %WM_KEYUP Then 'only looking for keyup to avoid multiple triggers while capturing hidDevice = raw.Header.hDevice 'set hid handle capturehid = %False 'reset capture flag End If Exit Function 'don't process key while capturing End If If hidDevice And raw.Header.hDevice <> hidDevice Then Exit Function 'only process specific hid if captured ztrace Str$(raw.keyboard.vkey) 'show all key numbers. Select Case raw.keyboard.vkey 'get key pressed 'example key #1 Case %VK_TAB If GetForegroundWindow <> HWND_FRMMAIN Then Exit Function 'tab key is only used if program is on top If (raw.keyboard.message And %WM_KEYDOWN) = %WM_KEYDOWN Then 'only act on keydown (multiple times if held down) ztrace "Tab key (down)" End If 'example key #2 Case %VK_F9 If (raw.keyboard.message And %WM_KEYUP) = %WM_KEYUP Then 'F9 keyup only resets hidF9 flag hidF9 = %False Exit Function ElseIf (raw.keyboard.message And %WM_KEYDOWN) = %WM_KEYDOWN Then 'F9 keydown is only recognized once until the hidF9 flag is reset If hidF9 = %False Then hidF9 = %True ztrace "F9 key (down)" End If End If 'example key #3 Case %Hid_Left 'Page Up 'presenter hid left key If (raw.keyboard.message And %WM_KEYUP) <> %WM_KEYUP Then Exit Function 'only act on keyup ztrace "hid left button (up)" End Select End Select End Function
Comment