Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoHotKey to accurately log keystrokes

Tags:

autohotkey

"I am trying to make an AutoHotKey program where I can play a game and it will record keypress/keyrelease events along with a millisecond timestamp so I can later repeat those exact actions in a secondary program. Ex. I play a game and the program records the keystrokes in time so that it can be replayed with a bot. This is what I have so far that detects the up arrow key being pressed or released. The issue is that I am not able to get the output to appear."

startTime=A_TickCount
currentTime=A_TickCount
UpState=0
While True{
Esc::
ExitApp
return
GetKeyState, state, Up
    if state==D && UpState==0
    {
        UpState=1
        currentTime=A_TickCount-startTime
        OutputDebug,("UP_PRESS: " %currentTime%)
    }
    else if state==U && UpState==1
    {
        UpState=0
        currentTime=A_TickCount-startTime
        OutputDebug,("UP_RELEASE: " %currentTime%)
    }
}

UPDATED: WORKING SCRIPT BELOW

$F1::
startTime :=A_TickCount
pTime:=A_TickCount
currentTime:=A_TickCount
FileDelete, play.txt
UpState:=0
DownState:=0
LeftState:=0
RightState:=0
SpaceState:=0
TabState:=0
qState:=0
wState:=0
eState:=0
rState:=0
Loop{
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Up
if (state="D" and UpState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    UpState=1
text:="`nPUP()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and UpState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    UpState=0
text:="`nRUP()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Down
if (state="D" and DownState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    DownState=1
text:="`nPDOWN()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and DownState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    DownState=0
text:="`nRDOWN()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Left
if (state="D" and LeftState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    LeftState=1
text:="`nPLEFT()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and LeftState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    LeftState=0
text:="`nRLEFT()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Right
if (state="D" and RightState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    RightState=1
text:="`nPRIGHT()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and RightState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    RightState=0
text:="`nRRIGHT()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Space
if (state="D" and SpaceState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    SpaceState=1
text:="`nPSPACE()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and SpaceState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    SpaceState=0
text:="`nRSPACE()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, Tab
if (state="D" and TabState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    TabState=1
text:="`nPTAB()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and TabState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    TabState=0
text:="`nRTAB()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, q
if (state="D" and qState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    qState=1
text:="`nPQ()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and qState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    qState=0
text:="`nRQ()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, w
if (state="D" and wState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    wState=1
text:="`nPW()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and wState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    wState=0
text:="`nRW()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, e
if (state="D" and eState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    eState=1
text:="`nPE()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and eState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    eState=0
text:="`nRE()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GetKeyState, state, r
if (state="D" and rState="0")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    rState=1
text:="`nPR()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
else if (state="U" and rState="1")
{
text:="`ntime.sleep("
FileAppend, %text%, play.txt
currentTime:=A_TickCount-pTime
FileAppend, %currentTime%, play.txt
text:="/1000)"
FileAppend, %text%, play.txt
    rState=0
text:="`nRR()"
    FileAppend, %text%, play.txt
pTime:=A_TickCount
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

}        
$F2::
ExitApp
~$Backspace::
FileDelete, play.txt

TEST SCRIPT

$F1::
start := A_TickCount
FileDelete, Replay.ahk
begintext:="$F3::`n"
endtext:="$F4::`n"
endtext2:="ExitApp`n"
FileAppend, %begintext%, Replay.ahk
lasttext2=1
text2=
time=0
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~q::
text1:="Sleep, " time "`n"
text2:="Send {q Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpq, 10
return
WaitForButtonUpq:
if GetKeyState("q") 
    return

text1:="Sleep, " time "`n"
text2:="Send {q Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpq, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~w::
text1:="Sleep, " time "`n"
text2:="Send {w Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpw, 10
return
WaitForButtonUpw:
if GetKeyState("w") 
return

text1:="Sleep, " time "`n"
text2:="Send {w Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpw, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~e::
text1:="Sleep, " time "`n"
text2:="Send {e Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpe, 10
return
WaitForButtonUpe:
if GetKeyState("e") 
return

text1:="Sleep, " time "`n"
text2:="Send {e Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpe, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~r::
text1:="Sleep, " time "`n"
text2:="Send {r Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpr, 10
return
WaitForButtonUpr:
if GetKeyState("r") 
return

text1:="Sleep, " time "`n"
text2:="Send {r Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpr, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Tab::
text1:="Sleep, " time "`n"
text2:="Send {Tab Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpTab, 10
return
WaitForButtonUpTab:
if GetKeyState("Tab") 
return

text1:="Sleep, " time "`n"
text2:="Send {Tab Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpTab, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Space::
text1:="Sleep, " time "`n"
text2:="Send {Space Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpSpace, 10
return
WaitForButtonUpSpace:
if GetKeyState("Space") 
return

text1:="Sleep, " time "`n"
text2:="Send {Space Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpSpace, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Up::
text1:="Sleep, " time "`n"
text2:="Send {Up Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpUp, 10
return
WaitForButtonUpUp:
if GetKeyState("Up") 
return

text1:="Sleep, " time "`n"
text2:="Send {Up Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpUp, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Down::
text1:="Sleep, " time "`n"
text2:="Send {Down Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpDown, 10
return
WaitForButtonUpDown:
if GetKeyState("Down") 
return

text1:="Sleep, " time "`n"
text2:="Send {Down Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpDown, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Left::
text1:="Sleep, " time "`n"
text2:="Send {Left Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpLeft, 10
return
WaitForButtonUpLeft:
if GetKeyState("Left") 
return

text1:="Sleep, " time "`n"
text2:="Send {Left Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpLeft, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
~Right::
text1:="Sleep, " time "`n"
text2:="Send {Right Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpRight, 10
return
WaitForButtonUpRight:
if GetKeyState("Right") 
return

text1:="Sleep, " time "`n"
text2:="Send {Right Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
FileAppend, %text1%, Replay.ahk
FileAppend, %text2%, Replay.ahk
lasttext2:=text2
}
SetTimer, WaitForButtonUpRight, off
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$F2::
FileAppend, %endtext%, Replay.ahk
FileAppend, %endtext2%, Replay.ahk
ExitApp

~$Backspace::
FileDelete, Replay.ahk
FileAppend, %begintext%, Replay.ahk
like image 682
Eric Hill Avatar asked Feb 06 '17 20:02

Eric Hill


1 Answers

I made this simple script named Record.ahk I think its easy to understand and improve in the way you want. The F1 keyboard key starts the recording and the F2 key finishes the recording. All inputs (and time intervals) are stored in another script named Play.ahk

Later, running Play.ahk, the F3 key starts playing the keys sequence and the F4 key finishes it. As you see, Input is used to get the pressed keys and A_TickCount is used to get the delay between the inputs.

endkeyslist={LButton}{RButton}{MButton}{XButton1}{XButton2}{WheelDown}{WheelUp}{WheelLeft}{WheelRight}{CapsLock}{__________}{Tab}{Enter}{Esc}{BS}{ScrollLock}{Del}{Ins}{Home}{End}{PgUp}{PgDn}{Up}{Down}{Left}{Right}{Numpad0}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}{NumpadDot}{NumpadIns}{NumpadEnd}{NumpadDown}{NumpadPgDn}{NumpadLeft}{NumpadClear}{NumpadRight}{NumpadHome}{NumpadUp}{NumpadPgUp}{NumpadDel}{NumLock}{NumpadDiv}{NumpadMult}{NumpadAdd}{NumpadSub}{NumpadEnter}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{F13}{F14}{F15}{F16}{F17}{F18}{F19}{F20}{F21}{F22}{F23}{F24}{LWin}{RWin}{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{Browser_Back}{Browser_Forward}{Browser_Refresh}{Browser_Stop}{Browser_Search}{Browser_Stop}{Browser_Search}{Browser_Favorites}{Browser_Home}{Volume_Mute}{Volume_Down}{Volume_Up}{Media_Next}{Media_Prev}{Media_Stop}{Media_Play_Pause}{Launch_Mail}{Launch_Media}{Launch_App1}{Launch_App2}{AppsKey}{PrintScreen}{CtrlBreak}{Pause}{Break}{Help}{Sleep}{Space}    

$F1::
start := A_TickCount
FileDelete, play.ahk
text0:="$F3:: `n"
FileAppend, %text0%, play.ahk
Loop
{
Input, key, L1 V, %endkeyslist%
IfInString, ErrorLevel, EndKey:
{
StringTrimleft, key, ErrorLevel, 7
}
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
text2:="Send {" key "} `n"    
FileAppend, %text1%, play.ahk
FileAppend, %text2%, play.ahk
}
return

$F2::
text3:="return `n"
FileAppend, %text3%, play.ahk
text4:="F4:: ExitApp"
FileAppend, %text4%, play.ahk
ExitApp   

A variation of the first script, this second code records how long the key is pressed (D and U states) using KeyWait and stores it in a log.txt file. It have a limitation: don't get multiple keyspresses at once.

endkeyslist={LButton}{RButton}{MButton}{XButton1}{XButton2}{WheelDown}{WheelUp}{WheelLeft}{WheelRight}{CapsLock}{__________}{Tab}{Enter}{Esc}{BS}{ScrollLock}{Del}{Ins}{Home}{End}{PgUp}{PgDn}{Up}{Down}{Left}{Right}{Numpad0}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}{NumpadDot}{NumpadIns}{NumpadEnd}{NumpadDown}{NumpadPgDn}{NumpadLeft}{NumpadClear}{NumpadRight}{NumpadHome}{NumpadUp}{NumpadPgUp}{NumpadDel}{NumLock}{NumpadDiv}{NumpadMult}{NumpadAdd}{NumpadSub}{NumpadEnter}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{F13}{F14}{F15}{F16}{F17}{F18}{F19}{F20}{F21}{F22}{F23}{F24}{LWin}{RWin}{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{Browser_Back}{Browser_Forward}{Browser_Refresh}{Browser_Stop}{Browser_Search}{Browser_Stop}{Browser_Search}{Browser_Favorites}{Browser_Home}{Volume_Mute}{Volume_Down}{Volume_Up}{Media_Next}{Media_Prev}{Media_Stop}{Media_Play_Pause}{Launch_Mail}{Launch_Media}{Launch_App1}{Launch_App2}{AppsKey}{PrintScreen}{CtrlBreak}{Pause}{Break}{Help}{Sleep}{Space}

$F1::
start := A_TickCount
FileDelete, log.txt

Loop
{
Input, key, L1 V, %endkeyslist%
IfInString, ErrorLevel, EndKey:
{
StringTrimleft, key, ErrorLevel, 7
}
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
text2:="Send {" key " Down} `n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt

KeyWait, %key% ;waits the key to be released
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
text2:="Send {" key " Up} `n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
}
return

$F2:: ExitApp

This 3rd script is a variation of the previous one, but it records simultaneous keypresses. Its based on a script found on AHK Help Docs and uses SetTimer combined with GetKeyState. This minimal example has only 2 keys (a and b): you would need to write the same for each key of your keyboard. I did add the condition lasttext2 != text2 to let the log file smaller, but if you want to send several {Key Down} instead (while the key was held), just remove this condition.

start := A_TickCount
FileDelete, log.txt
lasttext2=1
text2=
time=0
return

~a::
text1:="Sleep, " time "`n"
text2:="Send {a Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
text1:="Sleep, " time "`n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
lasttext2:=text2
}
SetTimer, WaitForButtonUpa, 10
return

WaitForButtonUpa:
if GetKeyState("a")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
text1:="Sleep, " time "`n"
text2:="Send {a Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
lasttext2:=text2
}
SetTimer, WaitForButtonUpa, off
return

~b::
text1:="Sleep, " time "`n"
text2:="Send {b Down} `n"
if (lasttext2 != text2)
{
time := A_TickCount - start
start := A_TickCount
text1:="Sleep, " time "`n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
lasttext2:=text2
}
SetTimer, WaitForButtonUpb, 10
return

WaitForButtonUpb:
if GetKeyState("b")  ; The button is still, down, so keep waiting.
return
; Otherwise, the button has been released.
text1:="Sleep, " time "`n"
text2:="Send {b Up} `n"
if (lasttext2 != text2)
{
time := (A_TickCount - start)
start := A_TickCount
text1:="Sleep, " time "`n"
FileAppend, %text1%, log.txt
FileAppend, %text2%, log.txt
lasttext2:=text2
}
SetTimer, WaitForButtonUpb, off
return
like image 79
Le____ Avatar answered Oct 20 '22 17:10

Le____