"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