Press-and-hold character input delay in full remap of HID remote
I am attempting to remap a USB wireless remote thingy that, by default, sends single-letter keyboard inputs to the OS and instead sent a "
Without the below script running, if I single-press the Up button on my remote, Windows would receive a 'w' key. If I press and hold the up button, I will get a single key, a short pause, then the keys again as if the keyboard character is being held down: "w.....wwwwwwwww" (thanks to Windows' Character Repeat Delay, I reckon)
With the script running, the same delay occurs, but only with my new key. My assumption prior to drafting this issue was my delay was coming from the inefficient script (the full script will end up having 14 buttons to translate), but now I am beginning to suspect that perhaps Windows is adding the delay before AHK/Interception is able to receive the key inputs.
Do you have any thoughts @evilC? Thanks!
#SingleInstance force
#Persistent
#include Lib\AutoHotInterception.ahk
global AHI := new AutoHotInterception()
keyboardId := AHI.GetKeyboardId(0x348F, 0x1010) ; HID Keyboard ID for Remote's USB Dongle
AHI.SubscribeKeyboard(keyboardId, true, Func("KeyEvent"))
KeyEvent(code,state){
;ToolTip % "Keyboard Key - Code: " code ", State: " state
if (code = 2)
{
SendInput t ; for testing
}
else if (code = 17) ; key: w
{
SendInput !{up} ; send the hotkey ALT + Up arrow
; SendInput u ; TESTING: just send a u
}
else if (code = 31) ; key: s
{
SendInput !{down} ; send the hotkey ALT + Down arrow
}
else if (code = 30) ; key: a
{
SendInput !{left} ; send the hotkey ALT + Left arrow
}
else if (code = 32) ; key d
{
SendInput !{right} ; send the hotkey ALT + Right arrow
}
else if (code = 38) ; key: l
{
ExitApp ; Testing: Map a key on the remote to quit the script
}
else
{
ToolTip % "Keyboard Key - Code: " code ", State: " state ; If input is unrecognized, let's output the code
}
}
^Esc::
ExitApp
Could do with clarification here It's the line "With the script running, the same delay occurs, but only with my new key" that is confusing me, specifically the "only with my new key" bit.
Key repeat is a tricky one, and it highly depends on how the application that is receiving inputs behaves. Key repeat only affects press events - that is, if you hold a key, you (normally) get presss, press, press, press while you hold the key, then one release at the end when you release the key. If your device behaves differently, it is off-spec and more difficult to deal with.
In general though, you can use code like this to morph repeated keys in various ways ie here is code that will filter key repeats
#SingleInstance force
#Persistent
#include Lib\AutoHotInterception.ahk
global AHI := new AutoHotInterception()
keyboardId := AHI.GetKeyboardId(0x348F, 0x1010) ; HID Keyboard ID for Remote's USB Dongle
AHI.SubscribeKeyboard(keyboardId, true, Func("KeyEvent"))
KeyEvent(code,state){
static states := {} ; Array used to hold current state of keys. STATIC is important!
if (state && states[code]){ ; evaluates to true if key is already held
return ; filter key repeat
}
if (code = 2)
{
SendInput t ;
}
else if (code = 17) ; key: w
{
SendInput !{up}
}
; Update the states array with the current state of this key
states[code] := state
}
If you want key repeat, but want to remove the delay, you can use similar code, but add a call to SetTimer to repeatedly send the key while it is held
This required some fixing for v2 and my use case. Here is the code
#Requires AutoHotkey v2.0
#SingleInstance Force
Persistent
#include AutoHotInterception\Lib\AutoHotInterception.ahk
; Init interception
global AHI := AutoHotInterception()
macroKeys := AHI.GetDeviceId(false, 0x04D9, 0x1603) ; YOUR X7 keyboard
; Subscribe in EXCLUSIVE mode (no passthrough)
AHI.SubscribeKeyboard(macroKeys, true, KeyEvent)
; Output action queue
global actionQueue := []
global repeatForbidden := [57, 56, 29]
global lastState := Map()
SetTimer ProcessQueue, 2
KeyEvent(code, state) {
global lastState
; Prevent repeat on key-down
if (state = 1 && lastState.Has(code) && lastState[code] = 1) {
for _, forbiddenCode in repeatForbidden {
if (code = forbiddenCode)
return
}
}
; enqueue tooltip so we see activity
actionQueue.Push({type:"tip", val:"Code:" code " State:" state})
; build remaps
if (state = 1) {
switch code
{
case 44: actionQueue.Push({type:"send", val:"^c"}) ; Z -> Ctrl+C
case 45: actionQueue.Push({type:"send", val:"^x"}) ; X -> Ctrl+X
case 46: actionQueue.Push({type:"send", val:"^v"}) ; C -> Ctrl+V
case 17: actionQueue.Push({type:"send", val:"{Up}"}) ; W
case 30: actionQueue.Push({type:"send", val:"{Left}"}) ; A
case 31: actionQueue.Push({type:"send", val:"{Down}"}) ; S
case 32: actionQueue.Push({type:"send", val:"{Right}"}) ; D
case 56: actionQueue.Push({type:"send", val:"{Ctrl down}"}) ; Alt -> Ctrl down
case 29: actionQueue.Push({type:"send", val:"{Shift down}"}) ; Ctrl -> Shift down
case 57: actionQueue.Push({type:"send", val:"{Alt down}{Tab down}"}) ; Space -> Alt+Tab
}
} else {
switch code {
case 56: actionQueue.Push({type:"send", val:"{Ctrl up}"}) ; Alt release
case 29: actionQueue.Push({type:"send", val:"{Shift up}"}) ; Ctrl release
case 57: actionQueue.Push({type:"send", val:"{Alt up}{Tab up}"}) ; Space release
}
}
lastState[code] := state
}
ProcessQueue(*) {
global actionQueue
Loop 5 {
if (actionQueue.Length = 0)
break
item := actionQueue.RemoveAt(1)
if (item.type = "send") {
Send item.val
}
else if (item.type = "tip") {
ToolTip item.val
SetTimer ClearTip, -800
}
}
}
ClearTip(*) {
ToolTip
}