Code: Select all
#NoEnv
#InstallKeybdHook
#KeyHistory 20 ; Sets the maximum number of keyboard and mouse events displayed by the KeyHistory window
SetBatchLines, -1
~Enter::
~NumpadEnter::
if WasKeyBeforeLastSevenChars("F3")
MsgBox F3 was pressed before the last 7 chars
else
MsgBox F3 was NOT pressed before the last 7 chars
return
WasKeyBeforeLastSevenChars(key)
{
static skipKeys := "|Enter|NumpadEnter|BackSpace|Tab|Alt|Shift|Control|LButton|RButton|MButton|" ; <<<--- Add more here as needed (non printing keys)
pos1 := (InStr(txt := ScriptInfo("KeyHistory"), "------------------------------------------------------------------------------------------------") + 111)
pos2 := InStr(txt, "Press [F5] to refresh.")
txt := StrReplace(SubStr(txt, pos1, pos2-pos1), A_Space)
Loop, Parse, txt, `n, `r
Loop, Parse, A_LoopField, %A_Tab%
switch A_Index
{
case 3:
if (A_LoopField = "d") ; Discard: key down events
continue 2
case 5:
if InStr(skipKeys, "|" . A_LoopField . "|") ; Discard: non printing keys
continue 2
keys .= A_LoopField . "|"
}
keys := RTrim(keys,"|")
lastKeysArr := StrSplit(keys, "|")
Loop % lastKeysArr.length() - 7
if (lastKeysArr[A_Index] = key)
return true
return false
}
ScriptInfo(Command)
{ ; https://www.autohotkey.com/boards/viewtopic.php?t=9656
static hEdit := 0, pfn, bkp
if !hEdit {
hEdit := DllCall("GetWindow", "ptr", A_ScriptHwnd, "uint", 5, "ptr")
user32 := DllCall("GetModuleHandle", "str", "user32.dll", "ptr")
pfn := [], bkp := []
for i, fn in ["SetForegroundWindow", "ShowWindow"] {
pfn[i] := DllCall("GetProcAddress", "ptr", user32, "astr", fn, "ptr")
DllCall("VirtualProtect", "ptr", pfn[i], "ptr", 8, "uint", 0x40, "uint*", 0)
bkp[i] := NumGet(pfn[i], 0, "int64")
}
}
if (A_PtrSize=8) { ; Disable SetForegroundWindow and ShowWindow.
NumPut(0x0000C300000001B8, pfn[1], 0, "int64") ; return TRUE
NumPut(0x0000C300000001B8, pfn[2], 0, "int64") ; return TRUE
} else {
NumPut(0x0004C200000001B8, pfn[1], 0, "int64") ; return TRUE
NumPut(0x0008C200000001B8, pfn[2], 0, "int64") ; return TRUE
}
static cmds := {ListLines:65406, ListVars:65407, ListHotkeys:65408, KeyHistory:65409}
cmds[Command] ? DllCall("SendMessage", "ptr", A_ScriptHwnd, "uint", 0x111, "ptr", cmds[Command], "ptr", 0) : 0
NumPut(bkp[1], pfn[1], 0, "int64") ; Enable SetForegroundWindow.
NumPut(bkp[2], pfn[2], 0, "int64") ; Enable ShowWindow.
ControlGetText, text,, ahk_id %hEdit%
return text
}