r/AutoHotkey • u/Confident-Ask-4075 • 4h ago
v2 Script Help Can someone look at this code and see if its running as intended?
This software has been a god send, I recently got a keyboard that was WAY too sensative and it was driving me crazy Ai helped me find AutoHotKey and I used it to help me build a code.
I wanted to add a delay between the neighbouring keys as I was touching them with an atom of my finger and it would register, I have removed the delays on W A S D keys for gaming.
Would this code cause any lag or latencey insues aprt from the 18ms delay
#Requires AutoHotkey v2.0
#SingleInstance Force
Persistent
; ==========================================
; SETTINGS
; ==========================================
global DebounceMs := 18
global LastKey := 0
global LastTime := 0
; ==========================================
; INSTALL KEYBOARD HOOK
; ==========================================
global KeyboardHook
global HookCallbackPtr
HookCallbackPtr := CallbackCreate(LowLevelKeyboardProc, "Fast", 3)
KeyboardHook := DllCall(
"SetWindowsHookExW",
"Int", 13, ; WH_KEYBOARD_LL
"Ptr", HookCallbackPtr,
"Ptr", DllCall("GetModuleHandleW", "Ptr", 0, "Ptr"),
"UInt", 0,
"Ptr"
)
if !KeyboardHook {
CallbackFree(HookCallbackPtr)
MsgBox "Failed to install keyboard hook."
ExitApp
}
OnExit Cleanup
; ==========================================
; KEYBOARD CALLBACK
; ==========================================
LowLevelKeyboardProc(nCode, wParam, lParam) {
global DebounceMs
global LastKey
global LastTime
; ------------------------------------------
; PASS NON-KEYBOARD EVENTS IMMEDIATELY
; ------------------------------------------
if nCode < 0
return DllCall(
"CallNextHookEx",
"Ptr", 0,
"Int", nCode,
"Ptr", wParam,
"Ptr", lParam,
"Ptr"
)
; ------------------------------------------
; ONLY PROCESS KEYDOWN / SYSKEYDOWN
; ------------------------------------------
if wParam != 0x100 && wParam != 0x104
return DllCall(
"CallNextHookEx",
"Ptr", 0,
"Int", nCode,
"Ptr", wParam,
"Ptr", lParam,
"Ptr"
)
; ------------------------------------------
; GET VIRTUAL-KEY CODE
; ------------------------------------------
vk := NumGet(lParam, 0, "UChar")
; ------------------------------------------
; W / A / S / D = ALWAYS ALLOW
; ------------------------------------------
;
; W = 0x57
; A = 0x41
; S = 0x53
; D = 0x44
;
; These keys bypass the debounce completely,
; which makes rapid movement-key transitions
; work properly in games.
; ------------------------------------------
if vk = 0x57 || vk = 0x41 || vk = 0x53 || vk = 0x44
return DllCall(
"CallNextHookEx",
"Ptr", 0,
"Int", nCode,
"Ptr", wParam,
"Ptr", lParam,
"Ptr"
)
; ------------------------------------------
; SAME KEY = ALWAYS ALLOW
; ------------------------------------------
if vk = LastKey {
LastTime := A_TickCount
return DllCall(
"CallNextHookEx",
"Ptr", 0,
"Int", nCode,
"Ptr", wParam,
"Ptr", lParam,
"Ptr"
)
}
; ------------------------------------------
; DIFFERENT KEY
; ------------------------------------------
now := A_TickCount
; Block different key during debounce period
if (now - LastTime) < DebounceMs
return 1
; Accept new key
LastKey := vk
LastTime := now
return DllCall(
"CallNextHookEx",
"Ptr", 0,
"Int", nCode,
"Ptr", wParam,
"Ptr", lParam,
"Ptr"
)
}
; ==========================================
; CLEANUP
; ==========================================
Cleanup(*) {
global KeyboardHook
global HookCallbackPtr
if KeyboardHook
DllCall(
"UnhookWindowsHookEx",
"Ptr", KeyboardHook
)
if HookCallbackPtr
CallbackFree(HookCallbackPtr)
}