Here is a new version of my Remote Info script. It combines TheGood's "Example 1" and "Make a Script" examples from the first page.
In this version you simply check the boxes to the left of the remote your wanting a code for and enter the optional prefix. The complete g-label appears in the "Label to Use in Script" box and the start of the g-label section appears in the large edit box below so you can finish the g-label and cut/paste it into your script.
The "+#" box is for the NumGet input, a value of 1 is needed for most remotes. To put it simply, increasing this value may cause the key press to be read differently but can also cause it to not be seen. You can experiment to see what setting is most effective with your remote. If the script seems to stall uncheck "-Up" and return the value to 1.
The "-Up" checkbox ignores key presses that are 0 or likely keyup events. This leaves the keydown event in the Label box instead of causing it to enter a 0.
Please be aware I don't claim to understand how everything in this script works, most of it is taken from TheGood's examples. I combined and edited the scripts to make it easier for people to get the keypresses from their "Other-HID" devices. Most everything I did was learned by trial and error so please let me know if something can be improved.
Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
;Must be in auto-execute section if I want to use the constants
#Include %A_ScriptDir%\AHKHID.ahk
Gui, Add, Tab2, Buttons, Keyboard||Mice|Other||?
;Create GUI to receive messages
Gui, Tab, 1
Gui, Add, ListView, x16 y40 w470 NoSortHdr LV0x10 Grid vlvwKeyb gLV_Event AltSubmit, Name|Type|SubType|Keyboard Mode|Number of Function Keys|Number of Indicators|Number of Keys Total
Gui, Tab, 2
Gui, Add, ListView, x16 y40 w470 NoSortHdr LV0x10 Grid vlvwMouse gLV_Event AltSubmit, Name|Mouse ID|Number of Buttons|Sample Rate
If bVista
LV_InsertCol(5, "", "Has Horizontal Wheel")
Gui, Tab, 3
Gui, Add, ListView, x16 y40 w470 NoSortHdr LV0x10 Checked Grid AltSubmit vlvwOther gLV_Event, Name|Vendor ID|Product ID|Version Number|Usage Page|Usage
;Set up the constants
AHKHID_UseConstants()
;Get count
iCount := AHKHID_GetDevCount()
;Retrieve info for each device
Loop %iCount% {
HID0 += 1
;Get device handle, type and name
HID%HID0%_Handle := AHKHID_GetDevHandle(HID0)
HID%HID0%_Type := AHKHID_GetDevType(HID0)
HID%HID0%_Name := AHKHID_GetDevName(HID0)
;Get device info
If (HID%HID0%_Type = RIM_TYPEMOUSE) {
HID%HID0%_ID := AHKHID_GetDevInfo(HID0, DI_MSE_ID)
HID%HID0%_Buttons := AHKHID_GetDevInfo(HID0, DI_MSE_NUMBEROFBUTTONS)
HID%HID0%_SampleRate := AHKHID_GetDevInfo(HID0, DI_MSE_SAMPLERATE)
If bVista ;Only supported in Windows Vista and higher
HID%HID0%_HWheel := AHKHID_GetDevInfo(HID0, DI_MSE_HASHORIZONTALWHEEL)
} Else If (HID%HID0%_Type = RIM_TYPEKEYBOARD) {
HID%HID0%_KBType := AHKHID_GetDevInfo(HID0, DI_KBD_TYPE)
HID%HID0%_KBSubType := AHKHID_GetDevInfo(HID0, DI_KBD_SUBTYPE)
HID%HID0%_KeyboardMode := AHKHID_GetDevInfo(HID0, DI_KBD_KEYBOARDMODE)
HID%HID0%_FunctionKeys := AHKHID_GetDevInfo(HID0, DI_KBD_NUMBEROFFUNCTIONKEYS)
HID%HID0%_Indicators := AHKHID_GetDevInfo(HID0, DI_KBD_NUMBEROFINDICATORS)
HID%HID0%_KeysTotal := AHKHID_GetDevInfo(HID0, DI_KBD_NUMBEROFKEYSTOTAL)
} Else
If (HID%HID0%_Type = RIM_TYPEHID) {
HID%HID0%_VendorID := AHKHID_GetDevInfo(HID0, DI_HID_VENDORID)
HID%HID0%_ProductID := AHKHID_GetDevInfo(HID0, DI_HID_PRODUCTID)
HID%HID0%_VersionNumber := AHKHID_GetDevInfo(HID0, DI_HID_VERSIONNUMBER)
HID%HID0%_UsagePage := AHKHID_GetDevInfo(HID0, DI_HID_USAGEPAGE)
HID%HID0%_Usage := AHKHID_GetDevInfo(HID0, DI_HID_USAGE)
}
}
;Add to listviews according to type
Loop %HID0% {
If (HID%A_Index%_Type = RIM_TYPEMOUSE) {
Gui, ListView, lvwMouse
If bVista ;Only supported in Windows Vista and higher
LV_Add("",HID%A_Index%_Name, HID%A_Index%_ID, HID%A_Index%_Buttons, HID%A_Index%_SampleRate, HID%A_Index%_HWheel)
Else LV_Add("",HID%A_Index%_Name, HID%A_Index%_ID, HID%A_Index%_Buttons, HID%A_Index%_SampleRate)
} Else If (HID%A_Index%_Type = RIM_TYPEKEYBOARD) {
Gui, ListView, lvwKeyb
LV_Add("", HID%A_Index%_Name, HID%A_Index%_KBType, HID%A_Index%_KBSubType, HID%A_Index%_KeyboardMode
, HID%A_Index%_FunctionKeys, HID%A_Index%_Indicators, HID%A_Index%_KeysTotal)
} Else
If (HID%A_Index%_Type = RIM_TYPEHID) {
Gui, ListView, lvwOther
LV_Add("", HID%A_Index%_Name, HID%A_Index%_VendorID, HID%A_Index%_ProductID, HID%A_Index%_VersionNumber
, HID%A_Index%_UsagePage, HID%A_Index%_Usage)
}
}
;Adjust column width
Gui, ListView, lvwMouse
Loop 5
LV_ModifyCol(A_Index+1, "AutoHdr")
LV_ModifyCol(1, 142)
Gui, ListView, lvwKeyb
Loop 7
LV_ModifyCol(A_Index+1, "AutoHdr")
LV_ModifyCol(1, 142)
Gui, ListView, lvwOther
Loop 6
LV_ModifyCol(A_Index, "AutoHdr")
LV_ModifyCol(1, 142)
Gui, Add, GroupBox, w40 h40 Center, +#
Gui, Add, GroupBox, x+5 w90 h40, Prefix
Gui, Add, GroupBox, x+5 w95 h40, Key Pressed
Gui, Add, GroupBox, x+5 w35 h40 Center, -Up
Gui, Add, GroupBox, x+5 w190 h40, Label to Use in Script
Gui, Add, Edit, xm+11 yp+15 w30 h40 R1 voffset goffset,
Gui, Add, UpDown, vMyUpDown Range1-10, 1
Gui, Add, Edit, x+15 w80 vprefix gprefix,
Gui, Add, Text, x+19 vkey, 000000000000
Gui, Add, CheckBox, xp+101 yp+3 w21 Checked vboxNo0 gNo0,
Gui, Add, Edit, xp+35 yp-3 w180 vlabel,
Gui, Add, Edit, xm+5 y+10 w470 h100 vcomp, Use this box to add your commands then copy/paste into your script.
Gui, Tab,
;Gui, Add, Button, xm+10 y+20 gReload, R
Gui, Tab, 4
Gui, Font, s10 cBlack, arial black
Gui, Add, Text, , AHKHID - An AHK implementation of the HID functions
Gui, Font, s8 cBlack, arial
Gui, Add, Text, y+3, Remote Info is an adaptation of the AHKHID project and examples from TheGood.
Gui, Font, s7 cBlack, arial
Gui, Add, Text, y+5 w470, Remote Info is designed to make it easy to find the key presses from "Other HID" devices and turn them into g-labels for your scripts. keyboard and mice information is listed in thier respective tabs for reference.
Gui, Add, Text, y+7 w470, Instructions:
Gui, Add, Text, y+3 w470, The "Other" tab contains three sections, The listview of all "Other-HID" devices connected to your computer, Options and key press retrieved line and an edit box you can use to complete your g-label section.
Gui, Add, Text, y+5 w470, Check the box to the left of the HID device you wish to use. Some devices will have more than one entry as seen by the Vendor ID-Product ID-Version #, you will likely need to check all that match.
Gui, Add, Text, y+5 w470, The "+#" box supplies input for the NumGet command. 1 is used for most devices but it may be useful to experiment for your device. If the script seem to stall uncheck "-Up" and return to 1.
Gui, Add, Text, y+5 w470, The "-Up" check box ignores labels that are "0" which are likely KeyUp commands.
Gui, Add, Text, y+5 w470, Prefix(Optional) is used to assign a custom prefix to the key press. This will allow the same key to be used for different g-labels such as "VLC_225" and "MPC_225".
Gui, Add, Text, y+5 w470, With the device checked in listview and your optional prefix added press a key on your HID device. The g-label will appear in the "Label to Use in Script" box and in the command edit box.
Gui, Add, Text, y+5 w470, You may finish adding your command in the edit box and cut/paste it to your script.
Gui, Show, y0, Remote Info
Gui, Submit, NoHide
Gui, +LastFound
hGui := WinExist()
;Intercept WM_INPUT messages
WM_INPUT := 0xFF
OnMessage(WM_INPUT, "InputMsg")
;Register Remote Control with RIDEV_INPUTSINK (so that data is received even in the background)
register:
r := AHKHID_Register(up%A_EventInfo%, usage%A_EventInfo%, hGui, RIDEV_INPUTSINK)
;Prefix
sPrefix := prefix
Return
InputMsg(wParam, lParam) {
Local devh, iKey, sLabel
Critical
;Get handle of device
devh := AHKHID_GetInputInfo(lParam, II_DEVHANDLE)
;Check for error
If (devh <> -1) ;Check that it is my HP remote
And (AHKHID_GetDevInfo(devh, DI_DEVTYPE, True) = RIM_TYPEHID)
And (AHKHID_GetDevInfo(devh, DI_HID_VENDORID, True) = vid)
And (AHKHID_GetDevInfo(devh, DI_HID_PRODUCTID, True) = pid)
And (AHKHID_GetDevInfo(devh, DI_HID_VERSIONNUMBER, True) = ver)
{
;Get data
iKey := AHKHID_GetInputData(lParam, uData)
;Check for error
If (iKey <> -1) {
;Get keycode (located at the 6th byte) %oset%
iKey := NumGet(uData,%oset%, "UChar")
;MsgBox, %iKey%
;Call the appropriate sub if it exists
sLabel := sPrefix "" iKey
If IsLabel(sLabel)
Gosub, %sLabel%
If BoxNo0 = 0
GoTo KeyLabel
If iKey = 0
Return
KeyLabel:
Guicontrol, , prefix, %sPrefix%
Guicontrol, , key, %iKey%
Guicontrol, , label, %sLabel%
Guicontrol, , comp, %sLabel%:`n`nReturn
Return
}
}
}
;AHKHIDGUI===========
exit:
GuiClose:
Gui, Destroy
ExitApp
Return
No0:
Gui, Submit, NoHide
Return
offset:
oset = offset
Return
prefix:
Gui, Submit, NoHide
GoTo, register
Return
Reload:
Reload
Return
GuiSize:
Anchor("MyTabs", "wh")
Anchor("lvwKeyb", "wh")
Anchor("lvwMouse", "wh")
Anchor("lvwOther", "wh")
Return
LV_Event:
Critical
;up4 :=
If InStr(ErrorLevel, "c", true)
{
AHKHID_Register( up%A_EventInfo%, usage%A_EventInfo%, hGui, RIDEV_REMOVE)
up%A_EventInfo% :=
usage%A_EventInfo% :=
;MsgBox, vid pid ver event error`n%vid% %pid% %ver% %A_GuiEvent% %ErrorLevel%`n`nup1 up2 up3 up4 up5`n%up1% %up2% %up3% %up4% %up5%`n`nu1 u2 u3 u4 u5`n%usage1% %usage2% %usage3% %usage4% %usage5%
Return
}
If InStr(ErrorLevel, "C", true)
{
LV_GetText(vid, A_EventInfo, 2)
LV_GetText(pid, A_EventInfo, 3)
LV_GetText(ver, A_EventInfo, 4)
LV_GetText(up%A_EventInfo%, A_EventInfo, 5)
LV_GetText(usage%A_EventInfo%, A_EventInfo, 6)
;MsgBox, vid pid ver event error`n%vid% %pid% %ver% %A_GuiEvent% %ErrorLevel%`n`nup1 up2 up3 up4 up5`n%up1% %up2% %up3% %up4% %up5%`n`nu1 u2 u3 u4 u5`n%usage1% %usage2% %usage3% %usage4% %usage5%
Goto register
}
Return
;Anchor by Titan, adapted by TheGood
;http://www.autohotkey.com/forum/viewtopic.php?p=377395#377395
Anchor(i, a = "", r = false) {
static c, cs = 12, cx = 255, cl = 0, g, gs = 8, gl = 0, gpi, gw, gh, z = 0, k = 0xffff, ptr
If z = 0
VarSetCapacity(g, gs * 99, 0), VarSetCapacity(c, cs * cx, 0), ptr := A_PtrSize ? "Ptr" : "UInt", z := true
If (!WinExist("ahk_id" . i)) {
GuiControlGet, t, Hwnd, %i%
If ErrorLevel = 0
i := t
Else ControlGet, i, Hwnd, , %i%
}
VarSetCapacity(gi, 68, 0), DllCall("GetWindowInfo", "UInt", gp := DllCall("GetParent", "UInt", i), ptr, &gi)
, giw := NumGet(gi, 28, "Int") - NumGet(gi, 20, "Int"), gih := NumGet(gi, 32, "Int") - NumGet(gi, 24, "Int")
If (gp != gpi) {
gpi := gp
Loop, %gl%
If (NumGet(g, cb := gs * (A_Index - 1)) == gp, "UInt") {
gw := NumGet(g, cb + 4, "Short"), gh := NumGet(g, cb + 6, "Short"), gf := 1
Break
}
If (!gf)
NumPut(gp, g, gl, "UInt"), NumPut(gw := giw, g, gl + 4, "Short"), NumPut(gh := gih, g, gl + 6, "Short"), gl += gs
}
ControlGetPos, dx, dy, dw, dh, , ahk_id %i%
Loop, %cl%
If (NumGet(c, cb := cs * (A_Index - 1), "UInt") == i) {
If a =
{
cf = 1
Break
}
giw -= gw, gih -= gh, as := 1, dx := NumGet(c, cb + 4, "Short"), dy := NumGet(c, cb + 6, "Short")
, cw := dw, dw := NumGet(c, cb + 8, "Short"), ch := dh, dh := NumGet(c, cb + 10, "Short")
Loop, Parse, a, xywh
If A_Index > 1
av := SubStr(a, as, 1), as += 1 + StrLen(A_LoopField)
, d%av% += (InStr("yh", av) ? gih : giw) * (A_LoopField + 0 ? A_LoopField : 1)
DllCall("SetWindowPos", "UInt", i, "UInt", 0, "Int", dx, "Int", dy
, "Int", InStr(a, "w") ? dw : cw, "Int", InStr(a, "h") ? dh : ch, "Int", 4)
If r != 0
DllCall("RedrawWindow", "UInt", i, "UInt", 0, "UInt", 0, "UInt", 0x0101) ; RDW_UPDATENOW | RDW_INVALIDATE
Return
}
If cf != 1
cb := cl, cl += cs
bx := NumGet(gi, 48, "UInt"), by := NumGet(gi, 16, "Int") - NumGet(gi, 8, "Int") - gih - NumGet(gi, 52, "UInt")
If cf = 1
dw -= giw - gw, dh -= gih - gh
NumPut(i, c, cb, "UInt"), NumPut(dx - bx, c, cb + 4, "Short"), NumPut(dy - by, c, cb + 6, "Short")
, NumPut(dw, c, cb + 8, "Short"), NumPut(dh, c, cb + 10, "Short")
Return, true
}