Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

AHKHID - An AHK implementation of the HID functions

  • Please log in to reply
456 replies to this topic
  • Members
  • 1 posts
  • Last active: Mar 22 2013 11:06 PM
  • Joined: 20 Mar 2013

Great stuff! 
I am using an old Marble Mouse and this script is optimal for my scrolling problem. But I can't get It to work :S

Would be great if someone could point me in the right direction :)

Getting these errors:

Error: Call to nonexistent function.

Specifically: HID_Register(1,2,hGui,RIDEV_INPUTSINK)
			076: Return
			079: mouse_Moved = n
			080: BlockInput,MouseMove
			081: MouseGetPos,m_x,m_y,winID,control
			082: WinGet,procName,ProcessName,ahk_id %winID%
			083: hw_m_target := DllCall("WindowFromPoint", "int", m_x, "int", m_y)
			084: GetScrollMOde()
			085: HID_Register(1 ,2, hGui, REDEV_INPUTSINK)
			086: Return
			089: ToolTip
			090: BlockInput,MouseMoveOff
			091: HID_Register(1,2,0,RIDEV_REMOVE)
			092: if mouse_Moved = n
			093: MouseClick,Middle
			094: Return



  • Members
  • 1 posts
  • Last active: Apr 05 2013 12:58 AM
  • Joined: 04 Apr 2013

 You need to  include the AHKHID.ahk, or just do what I do and paste the whole thing into the top of your code :)



Great stuff! 
I am using an old Marble Mouse and this script is optimal for my scrolling problem. But I can't get It to work confused.png

Would be great if someone could point me in the right direction happy.png

Getting these errors:

Error: Call to nonexistent function.

Specifically: HID_Register(1,2,hGui,RIDEV_INPUTSINK)


  • Members
  • 3 posts
  • Last active: Feb 11 2015 11:44 PM
  • Joined: 05 May 2013

Re Moved...

  • Members
  • 1 posts
  • Last active: May 13 2013 04:13 AM
  • Joined: 10 May 2013



First of all great work. I've been trying to accomplish a pretty simple hack. I have a keyboard that has a Fn key usually where you usually have a windows key and I seem to be missing something really basic. This is the code I mashed together but whenever I try to use it, nothing happens. Does anybody have any idea what I'm missing?



Fn Key                255


;Must be in auto-execute section if I want to use the constants
#Include C:\Program Files\AutoHotkey\Lib\AHKHID.ahk

;Create GUI to receive messages
Gui, +LastFound
hGui := WinExist()

;Intercept WM_INPUT messages
OnMessage(WM_INPUT, "InputMsg")

;Register Remote Control with RIDEV_INPUTSINK (so that data is received even in the background)
r := AHKHID_Register(1, 6, hGui, RIDEV_INPUTSINK)

;Prefix loop
Loop {
    Sleep 1000
    If WinActive("ahk_class QWidget") Or WinActive("ahk_class VLC DirectX")
        sPrefix := "VLC"
    Else If WinActive("ahk_class Winamp v1.x") Or WinActive("ahk_class Winamp Video")
        sPrefix := "Winamp"
    Else If WinActive("ahk_class MediaPlayerClassicW")
        sPrefix := "MPC"
    Else sPrefix := "Default"


InputMsg(wParam, lParam) {
    Local devh, iKey, sLabel
    ;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) = 1118)
        And (AHKHID_GetDevInfo(devh, DI_HID_PRODUCTID, True) = 109)
        And (AHKHID_GetDevInfo(devh, DI_HID_VERSIONNUMBER, True) = 272) {
        ;Get data
        iKey := AHKHID_GetInputData(lParam, uData)
        ;Check for error
        If (iKey <> -1) {
            ;Get keycode (located at the 6th byte)
            iKey := NumGet(uData, 5, "UChar")
            ;Call the appropriate sub if it exists
            sLabel := sPrefix "_" iKey
            If IsLabel(sLabel)
                Gosub, %sLabel%

Default_255: ;Fn Key
    SendInput #

Thanks for your help.

  • Members
  • 12 posts
  • Last active: Jul 14 2013 03:46 AM
  • Joined: 19 Dec 2011

Has anyone had success with the newer Apple remote (the aluminium one) ? Out of the half dozen keyboards/mice I have listed, it doesn't seem to respond. So, am just wondering if I'm attempting the really difficult, or am just not doing it right. I'm using the IR receiver that came with a generic MCE remote, whos keys have been modified beyond all recognition with AHK. 

  • Members
  • 3 posts
  • Last active:
  • Joined: 14 Dec 2005

Great thread.

The AHK scripts I've seen rely on HID RawInput apis , and try as I might, I couldn't get it to work with my vec foot pedal.  


I ended up creating an AHK script which talks to a custom app I wrote which uses signal11's hidapi library to communicate with the HID devices.



another option to try if you can't get your device working...




  • Members
  • 1 posts
  • Last active: Jun 19 2013 04:44 PM
  • Joined: 19 Jun 2013



Does anyone know if i could use something like this to simulate a HID event into windows without a device plugged in?

I.E. simulating a vol up/vol down on a headset.

Or having the actual HID device plugged in and then sending a HID message to windows without physically pressing button on device.





  • Members
  • 2 posts
  • Last active: Jul 10 2013 09:31 PM
  • Joined: 10 Jul 2013

Autohotkey novice here, with novice questions =)


How do I load the AHKHID?  I've tried just copying/pasting into notepad, and running it as an .ahk, but that doesn't seem to work.


I recently bought a Surface Pro, and am trying to remap the Wacom pen's buttons.  This thread seems to be the thing I need to understand, but unfortunately I don't understand it.  Can someone help me out? 


I'm essentially trying to make it so that I can use the pen to toggle the inking (send !JIP or !JIX) feature in Microsoft Word.  I've tried simply remapping a double right click or right click hold, and my scripts work when I use the touchpad or a mouse, but not when I'm using the pen.  I assume the problem is that the wacom pen sends virtual input that AHK is not detecting. 


Can someone help me out?

  • Members
  • 2 posts
  • Last active: Jul 10 2013 09:31 PM
  • Joined: 10 Jul 2013

nevermind - got the script to work.  I've tried using example 2, but I have no idea what all of the strings mean.


So now my questions are:

1) how do I identify the Wacom Pen?  I see strings like:


under the "Other" tab:

Name:     ?\HID#VID_045E&PIC_079C&MI ............. 

Vendor ID: 1118 

Product ID: 1948

and so on.  Can someone explain these to me?


2) how can I see input made using the pen? 


3) how do I translate that input into something an AHK script would recognize?


Logically, those seem like the things I need in order to make my script work, but if I'm totally off, please let me know.  I have a feeling this is much more complicated =)  Also, there's a lot of discussion of "registering" devices in the comments for the scripts - does that mean I need to somehow register the devices in AHK?



Any help would be awesome.  Thanks everyone - seems like a great community!

  • Members
  • 8 posts
  • Last active: Sep 30 2013 01:11 AM
  • Joined: 24 Sep 2013

Hi all,


I had a question here... i moved it.


  • Members
  • 8 posts
  • Last active: Sep 30 2013 01:11 AM
  • Joined: 24 Sep 2013

Jut thought i'd put this out there for the ppl breaking their brain, trying to do the 2 keyboard thing:




AHK rules...

  • Members
  • 108 posts
  • Last active: Mar 16 2014 08:02 PM
  • Joined: 22 Jan 2009

does injecting HID trick direct3d games etc?

  • Members
  • 2 posts
  • Last active: Nov 27 2013 03:58 AM
  • Joined: 21 Feb 2013

I'm trying to understand why Micha's dll implementation is able to detect my hid devices while AHKHID and the example scripts report nothing, after including ahkhid.ahk. AHKHID_GetDevCount() always returns -1. Is there some special dependancy for calling GetRawInputDeviceList? My system is winxp sp3. Thanks a lot!

  • Members
  • 6 posts
  • Last active: Dec 06 2013 09:58 PM
  • Joined: 24 Nov 2013

I got a usb keyboard I want to use with these scripts.


Tried running example1, using #include AHKHID.ahk as first line


It runs, but I don't see any keyboards/mice/anything on the devices, did I miss something ?

  • Members
  • 1 posts
  • Last active: Jan 29 2014 09:54 AM
  • Joined: 24 Jan 2014

I wanted to share my solution to the following problem.


I own a Macbook with a bad keyboard, that randomly starts pressing the "arrow up" key.

Because this is an old laptop, I didn't want to invest any money on a new keyboard (which costs more than the laptop).

So I've decided to try and disable the "arrow up" key on the apple's keyboard whilst keeping the functionality on all other keyboards connected.


On OS X I've found a great program called keyremap4macbook, which allowed to map the key to "null" for a specific device. (This is acommplished with the <device_only> tag for their xmls).

But on Windows I couldn't find something equivalent.


I saw many people trying to use AutoHotKey and HIDMacros / AHKHID to accomplish similar needs, but I couldn't find a solution (perhaps HIDMacros does work, but on Windows 7 x64 it crashes, so I didn't make too much effort on that direction).


I've played a little with AHKHID and with the help of the AmourSpirit's comment, I was able to distinguish between the Apple's keyboard and the rest of the world (different number of key count).

But, as stated on main thread, it is not possible to block the key strokes with AHKHID. Trying to do the following:


Which results in blocking the up key for all keyboards. Then I thought I might be able to tap on the key strokes with AHKHID, but I guess the mapping is down on a lower level, because the strokes don't get to the message loop.


So I've looked for a solution for the strokes to get to the script but not to the rest of the system. It first I thought about trying to block the up key from the message loop itself (with Hotkey remapping), this works only for the "next" stroke, which renders this solution useless.


So I looked up for another solution to remap the keys. I've came across SharpKeys. This program simply wraps up registry changes to remap specific keys. So I've mapped the up arrow to some unknown keycode (specifically to 0xE0_55)


Now I was able to use my script to tap on those strokes and simulate an up key click:

#Include %A_ScriptDir%\AHKHID.ahk

;Create GUI to receive messages
Gui, +LastFound
hGui := WinExist()


LocalScript_Constants() {
    Global ;To make the constants global
    VK_SHIFT   := 0x10
    VK_CONTROL := 0x11
    VK_ALT     := 0x12
    VK_LWIN    := 0x5b
    VK_RWIN    := 0x5c

    WM_KEYFIRST     := 0x0100
    WM_KEYDOWN      := 0x0100
    WM_KEYUP        := 0x0101
    WM_CHAR         := 0x0102
    WM_DEADCHAR     := 0x0103
    WM_SYSKEYDOWN   := 0x0104
    WM_SYSKEYUP     := 0x0105
    WM_SYSCHAR      := 0x0106
    WM_SYSDEADCHAR  := 0x0107

    g_shiftDown := ""
    g_ctrlDown  := ""
    g_lwinDown   := ""
    g_rwinDown   := ""
    g_altDown   := "" ; Alt behaves a little differently (also affects the msg)


;Intercept WM_INPUT messages
OnMessage(WM_INPUT, "InputMsg")

;Register keyboard with RIDEV_INPUTSINK (so that data is received even in the background)
r := AHKHID_Register(1, 6, hGui, RIDEV_INPUTSINK)


InputMsg(wParam, lParam) {
    Local r, h
    Critical    ;Or otherwise you could get ERROR_INVALID_HANDLE
    ;Get device type
    r := AHKHID_GetInputInfo(lParam, II_DEVTYPE) 
    ;Check for error
        h := AHKHID_GetInputInfo(lParam, II_DEVHANDLE)
        iKeyCount := AHKHID_GetDevInfo(h, DI_KBD_NUMBEROFKEYSTOTAL, True)
        vKey := AHKHID_GetInputInfo(lParam, II_KBD_VKEY)
        msg := AHKHID_GetInputInfo(lParam, II_KBD_MSG)
        ; OutputDebug %vKey%_%msg%
        If (iKeyCount = -1) {
            ; OutputDebug Simulated key %vKey%_%msg%
        If (vKey = VK_SHIFT)
            g_shiftDown := IsKeyDown(msg) ? "{Shift Down}" : ""
        Else If (vKey = VK_CONTROL)
            g_ctrlDown  := IsKeyDown(msg) ? "{Ctrl Down}" : ""
        Else If (vKey = VK_LWIN)
            g_lwinDown  := IsKeyDown(msg) ? "{LWin Down}" : ""
        Else If (vKey = VK_RWIN)
            g_rwinDown  := IsKeyDown(msg) ? "{RWin Down}" : ""
        Else If (vKey = VK_ALT)
            g_altDown    := IsKeyDown(msg) ? "{Alt Down}" : ""
        Else If (iKeycount > 0 && iKeyCount ^= 265) {
            ; Remaps broken keys back to real keys
            downLabel := IsKeyDown(msg) ? "DOWN" : "UP"
            sLabel := "NOT_APPLE_" vKey "_" downLabel 
            ; OutputDebug %sLabel%
            If IsLabel(sLabel) {
                Gosub, %sLabel%
        Else {
            ; OutputDebug An apple key: %vKey%_%msg%

    ; OutputDebug %g_lwinDown%%g_rwinDown%%g_shiftDown%%g_ctrlDown%%g_altDown%{Up Down}
    SendInput, %g_lwinDown%%g_rwinDown%%g_shiftDown%%g_ctrlDown%%g_altDown%{Up Down}

    SendInput {Up Up}

IsKeyDown(msg) {
    Return msg = WM_KEYDOWN || msg = WM_SYSKEYDOWN

This solution works. The page is no longer scrolling spontaneously up. :)

Note that I had to simulate the modifier key strokes, because programs did not catch them with the simulated key (Most probably is because the combination they actually get is: "Shift Down, Unknown Key Down, Up Key Down" and they don't handle it well)


I hope this helps anyone.




Fixed a little bug with alt (didn't realize holding alt makes WM_SYSKEYDOWN messages instead of WM_KEYDOWN)