Ahkhid Ahk
Ahkhid Ahk
TheGood
AHKHID - An AHK implementation of the HID functions.
Last updated: August 22nd, 2010
If you explicitly #include AHKHID in your script, you will have all the constants
available to you. Otherwise, if AHKHID is
in your library folder and you do not wish to explicitly #include it, you can call
AHKHID_UseConstants() to have the
constants available to you.
FUNCTION LIST:
_____________________
AHKHID_UseConstants()
You don't have to call this function manually. It is automatically called by other
functions to get the pointer of the
RAWINPUTDEVICELIST struct array. However, if a new device is plugged in, you will
have to refresh the listing by calling it
with bRefresh = True. Returns -1 on error (with error message in ErrorLevel).
____________________
AHKHID_GetDevCount()
Returns the index (starts at 1) of the device in the enumeration with matching
handle.
Returns 0 if not found.
______________________________________
AHKHID_GetDevType(i, IsHandle = False)
Returns the type of the device. See the RIM_ constants for possible values.
If IsHandle is false, then i is considered the index (starts at 1) of the device in
the enumeration.
Otherwise it is the handle of the device.
______________________________________
AHKHID_GetDevName(i, IsHandle = False)
Returns the name of the device (or empty string on error, with error message in
ErrorLevel).
If IsHandle is false, then i is considered the index (starts at 1) of the device in
the enumeration.
Otherwise it is the handle of the device.
____________________________________________
AHKHID_GetDevInfo(i, Flag, IsHandle = False)
Retrieves info from the RID_DEVICE_INFO struct. To retrieve a member, simply use
the corresponding flag. A list of flags
can be found at the top of the script (the constants starting with DI_). Each flag
corresponds to a member in the struct.
If IsHandle is false, then i is considered the index (starts at 1) of the device in
the enumeration. Otherwise it is the
handle of the device. Returns -1 on error (with error message in ErrorLevel).
You will need to do this if you want to use advance features of the RAWINPUTDEVICE
flags. For example, if you want to
register all devices using Usage Page 1 but would like to exclude devices of Usage
Page 1 using Usage 2 (keyboards), then
you need to place two elements in the array. The first one is
AHKHID_AddRegister(1,0,MyGuiHandle,RIDEV_PAGEONLY) and the
second one is AHKHID_AddRegister(1,2,MyGuiHandle,RIDEV_EXCLUDE).
Tip: Have a look at all the flags you can use (see the constants starting with
RIDEV_). The most useful is RIDEV_INPUTSINK.
Tip: Set Handle to 0 if you want the WM_INPUT messages to go to the window with
keyboard focus.
Tip: To unregister, use the flag RIDEV_REMOVE. Note that you also need to use the
RIDEV_PAGEONLY flag if the TLC was
registered with it.
____________________________________________________________________________
AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0)
This function can be used in two ways. If no parameters are specified, it will use
the RAWINPUTDEVICE array created through
AHKHID_AddRegister() and register. Otherwise, it will register only the specified
parameters. For example, if you just want
to register the mouse, you can simply do AHKHID_Register(1,2,MyGuiHandle). Returns
0 on success, returns -1 on error (with
error message in ErrorLevel).
This function allows you to get an array of the TLCs that have already been
registered.
It fills uDev with an array of RAWINPUTDEVICE and returns the number of elements in
the array.
Returns -1 on error (with error message in ErrorLevel).
This function is used to retrieve the data upon receiving WM_INPUT messages. By
passing the lParam of the WM_INPUT (0xFF00)
messages, it can retrieve all the members of the RAWINPUT structure, except the raw
data coming from HID devices (use
AHKHID_GetInputData for that). To retrieve a member, simply specify the flag
corresponding to the member you want, and call
the function. A list of all the flags can be found at the top of this script (the
constants starting with II_). Returns -1
on error (with error message in ErrorLevel).
See Example 2 for an example on how to use it to retrieve each member of the
structure.
See Example 3 for an example on how to interpret members which represent flags.
Tip: You have to use Critical in your message function or you might get invalid
handle errors.
Tip: You can check the value of wParam to know if the application was in the
foreground upon reception (see RIM_INPUT).
_____________________________________________
AHKHID_GetInputData(InputHandle, ByRef uData)
This function is used to retrieve the data sent by HID devices of type RIM_TYPEHID
(ie. neither keyboard nor mouse) upon
receiving WM_INPUT messages. CAUTION: it does not check if the device is indeed of
type HID. It is up to you to do so (you
can use GetInputInfo for that). Specify the lParam of the WM_INPUT (0xFF00) message
and the function will put in uData the
raw data received from the device. It will then return the size (number of bytes)
of uData. Returns -1 on error (with error
message in ErrorLevel).
See Example 2 for an example on how to use it (although you need an HID device of
type RIM_TYPEHID to test it).
*/
AHKHID_Included := True
AHKHID_SetConstants:
;______________________________________
;Flags you can use in AHKHID_GetDevInfo
;http://msdn.microsoft.com/en-us/library/ms645581
DI_DEVTYPE := 4 ;Type of the device. See RIM_ constants.
If Not AHKHID_Included
Return
AHKHID_UseConstants() {
Global ;To make the constants global
AHKHID_Included := False
Gosub, AHKHID_SetConstants
}
AHKHID_Initialize(bRefresh = False) {
Static uHIDList, bInitialized := False
;Prep var
VarSetCapacity(uHIDList, iCount * (A_PtrSize * 2))
r := DllCall("GetRawInputDeviceList", "Ptr", &uHIDList, "UInt*", iCount,
"UInt", A_PtrSize * 2)
If (r = -1) Or ErrorLevel {
ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r
%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
Return -1
}
bInitialized := True
Return &uHIDList
}
AHKHID_GetDevCount() {
AHKHID_GetDevHandle(i) {
Return NumGet(AHKHID_Initialize(), (i - 1) * (A_PtrSize * 2))
}
AHKHID_GetDevIndex(Handle) {
Loop % AHKHID_GetDevCount()
If (NumGet(AHKHID_Initialize(), (A_Index - 1) * (A_PtrSize * 2)) = Handle)
Return A_Index
Return 0
}
Return s
}
;Retrieve data
Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt")
}
Return 0
}
;Put in struct
NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
NumPut(Usage, uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
NumPut(Flags, uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
NumPut(Handle, uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
Return &uDev
}
;Call
r := DllCall("RegisterRawInputDevices", "Ptr", AHKHID_AddRegister(),
"UInt", AHKHID_AddRegister("Count"), "UInt", 8 + A_PtrSize)
;Prep var
VarSetCapacity(uDev, (8 + A_PtrSize), 0)
;Call
r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8
+ A_PtrSize)
Return 0
}
AHKHID_GetRegisteredDevs(ByRef uDev) {
;Get length
VarSetCapacity(iCount, 4, 0)
r := DllCall("GetRegisteredRawInputDevices", "Ptr", 0, "UInt*", iCount, "UInt",
8 + A_PtrSize)
If ErrorLevel {
ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r
%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
Return -1
}
If (iCount > 0) {
;Prep var
VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
;Call
r := DllCall("GetRegisteredRawInputDevices", "Ptr", &uDev, "UInt*", iCount,
"UInt", 8 + A_PtrSize)
If (r = -1) Or ErrorLevel {
ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value:
%r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
Return -1
}
}
Return iCount
}
AHKHID_GetInputInfo(InputHandle, Flag) {
Static uRawInput, iLastHandle := 0
;Prep var
VarSetCapacity(uRawInput, iSize)
;Retrieve data
Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ?
(AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int"
: (Flag = 8 ? "Ptr" : "UInt")))
}
Return 0
}
;Prep var
VarSetCapacity(uRawInput, iSize)
;Get the size of each HID input and the number of them
iSize := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
iCount := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
;Allocate memory
VarSetCapacity(uData, iSize * iCount)
;Copy bytes
DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 +
8, UInt, iSize * iCount)