Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
; 监测正在使用的键盘或鼠标.ahk
; https://autohotkey.com/board/topic/21283-native-hid-support/?p=139818
#NoEnv
; Disable this line if you're running from an editor that catches StdOut
; (like PSPad or SciTE.)
DllCall("AllocConsole")
OnMessage(0xFF, "WM_INPUT")
; 6, 1 = keyboard
; 2, 1 = mouse
Usage = 2
UsagePage = 1
VarSetCapacity(dev, 12, 0)
NumPut(UsagePage, dev, 0, "UShort")
NumPut(Usage, dev, 2, "UShort")
NumPut(0x100, dev, 4) ; dwFlags = RIDEV_INPUTSINK (don't require foreground)
NumPut(A_ScriptHwnd, dev, 8)
ret := DllCall("RegisterRawInputDevices"
, "uint", &dev ; pRawInputDevices (pointer to an array of RAWINPUTDEVICE)
, "uint", 1 ; uiNumDevices
, "uint", 12) ; cbSize (size of a RAWINPUTDEVICE structure)
if (ErrorLevel or !ret) {
MsgBox, RegisterRawInputDevices failed.
ExitApp
}
return
WM_INPUT(wParam, lParam)
{
Critical
; foreground := ! (wParam & 0xFF)
; Get required buffer size.
DllCall("GetRawInputData", "uint", lParam, "uint", 0x10000003
, "uint", 0, "uint*", size, "uint", 16)
VarSetCapacity(raw, size, 0)
; Get raw input data from handle (lParam)
ret := DllCall("GetRawInputData", "uint", lParam, "uint", 0x10000003
, "uint", &raw, "uint*", size, "uint", 16, "int")
if (ErrorLevel or ret = -1) {
StdOut("GetRawInputData -- Error " ErrorLevel " LA " A_LastError)
return
}
type := NumGet(raw)
; #define RIM_TYPEMOUSE 0
; #define RIM_TYPEKEYBOARD 1
; #define RIM_TYPEHID 2
hDevice := NumGet(raw, 8)
if type = 1 ; RIM_TYPEKEYBOARD
{
SetFormat, INTEGER, H
sc := NumGet(raw, 16, "UShort")
flags := NumGet(raw, 18, "UShort")
vk := NumGet(raw, 22, "UShort")
msg := NumGet(raw, 24, "UInt") ; WM_KEYDOWN, WM_SYSKEYDOWN, etc.
vk_a := SubStr(vk, 3)
sc_a := SubStr(sc, 3)
StringUpper, vk_a, vk_a
StringUpper, sc_a, sc_a
if (StrLen(vk_a)<2)
vk_a = 0%vk_a%
sc_a := ((flags & 2) ? "1" : "0") . (StrLen(sc_a)<2 ? "0" : "") . sc_a
act := (flags & 1) ? "up" : "dn"
StdOut(vk_a " " sc_a " " act " msg: " msg " flags: " flags " dev: " hDevice)
}
else if type = 2 ; RIM_TYPEHID
{
SetFormat, INTEGER, H
count := NumGet(raw, 20) ; dwCount
if count > 1
StdOut("dev: " hDevice)
n = 0
Loop, %count%
{
size := NumGet(raw, 16) ; dwSizeHid
VarSetCapacity(hex, size*3)
Loop, %size%
{
char := SubStr(NumGet(raw, 24+n+A_Index-1, "UChar"), 3)
StringUpper, char, char
if (StrLen(char) < 2)
char = 0%char%
hex = %hex% %char%
}
n += size
if !size
hex = "--"
StdOut((count=1 ? "dev: " hDevice " -- " : "") hex)
}
}
else if type = 0 ; RIM_TYPEMOUSE
{
devName := AHKHID_GetDevName(hDevice)
lastX := NumGet(raw, 28, "int")
lastY := NumGet(raw, 32, "int")
delta := NumGet(raw, 22, "Short") ; usButtonData
SetFormat, INTEGER, H
flags := NumGet(raw, 16, "UShort") ; usFlags
btnfl := NumGet(raw, 20, "UShort") ; usButtonFlags
StdOut("dev: " hDevice " flags: " flags " x" lastX " y" lastY " btnfl: " btnfl " delta: " delta "`nname: " devName "`n")
}
}
StdOut(ByRef text) {
FileAppend, %text%`n, *
}
AHKHID_GetDevName(hDevice) {
;Get device name length. RIDI_DEVICENAME
r := DllCall("GetRawInputDeviceInfo", "Ptr", hDevice, "UInt", 0x20000007, "Ptr", 0, "UInt*", iLength)
If (r = -1) Or ErrorLevel {
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
Return ""
}
;Get device name.
VarSetCapacity(s, (iLength + 1) * 2) ;RIDI_DEVICENAME
r := DllCall("GetRawInputDeviceInfo", "Ptr", hDevice, "UInt", 0x20000007, "Str", s, "UInt*", iLength)
If (r = -1) Or ErrorLevel {
ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
Return ""
}
Return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.