This is part of a larger automation script I've been hacking away at for some time.
I found your post, and was inspired by it to try GetKeyboardState, but as you can see, I learned that it is reading the keyboard state of the AHK v2 process and not the process I was interested in. I think this was probably your problem.
I included the relevant code to update the title bar because I found this to be an interesting method of debugging keystate-related issues (like, why are some prefix keys stuck down sometimes? Although I haven't used it long enough to debug such issues much yet...)
Code: Select all
Debug_KeyState := true
;...
GetAllKeyState() {
freq := 0
CounterBefore := 0
CounterAfter := 0
;Critical "On" ;; testing to see if the timing would be more stable
;~ DllCall("QueryPerformanceFrequency", "Int64*", &freq)
;~ DllCall("QueryPerformanceCounter", "Int64*", &CounterBefore)
;~ kss := ""
kp := "" ; physical key states
kl := "" ; logical key states
kb := "" ; both
Loop 255 {
vk := Format("vk{:X}", A_Index)
n := GetKeyName(vk)
p := GetKeyState(vk, "P")
l := GetKeyState(vk)
;; display vk only if not [0-9A-Z]
if A_Index >= 48 && A_Index <= 57 || A_Index >= 65 && A_Index <= 90 {
ks := n . " "
} else {
ks := n . "(" . vk . ") "
}
if p && l && p == l {
kb .= ks
} else if l && !p {
kl .= ks
} else if p && !l {
kp .= ks
}
}
kss := ""
if StrLen(kb) {
kss .= " keyState:" . kb
}
if StrLen(kp) {
kss .= "Phys:" . kp
}
if StrLen(kl) {
kss .= " Logi:" . kl
}
;~ if StrLen(kss)>0 {
;~ DllCall("QueryPerformanceCounter", "Int64*", &CounterAfter)
;~ kss .= "" . Format("{:d} µs", (CounterAfter - CounterBefore) * 1000000 / freq)
;~ }
;Critical "Off"
return kss
}
~^!Home::ClearAllKeyState()
ClearAllKeyState() {
WinSetTitle("Clearing key state", "ahk_exe GTA5.exe")
Loop 255 {
vk := Format("\{vk{:X} UP\}", A_Index)
Send(vk)
}
}
global current_keystate := ""
global last_keystate := ""
updateTitleBar() {
global last_keystate
if last_keystate == "suspended" {
return
}
if last_keystate != current_keystate {
title := GTAtitle . "`: " . current_keystate
WinSetTitle(title, "ahk_exe GTA5.exe")
last_keystate := current_keystate
}
}
UpdateTitleBar_KeyState() {
global current_keystate := GetAllKeyState()
updateTitleBar()
}
if Debug_KeyState {
SetTimer(UpdateTitleBar_KeyState,1000,0)
}
Return
For more about keystate, see
https://github.com/tallpeak/AHK
and a recent stream on twitch.tv/sqlexpert