AutoHotKey可准确记录击键

时间:2017-02-06 20:27:00

标签: autohotkey

"我正在尝试创建AutoHotKey程序,我可以在其中播放游戏,它会记录按键/键盘释放事件以及毫秒时间戳,以便稍后我可以在辅助程序中重复这些确切的操作。 防爆。我玩游戏,程序及时记录击键,以便可以用机器人重放。 这是我到目前为止检测到按下或释放的向上箭头键。问题是我无法显示输出。"

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%)
    }
}

更新:以下工作脚本

$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

1 个答案:

答案 0 :(得分:2)

我创建了一个名为Record.ahk的简单脚本。我认为它很容易理解并以您想要的方式进行改进。 F1键盘键开始录音,F2键完成录音。所有输入(和时间间隔)都存储在另一个名为Play.ahk

的脚本中

稍后,运行Play.ahk,F3键开始播放键序列,F4键完成。如您所见,Input用于获取按下的键,A_TickCount用于获取输入之间的延迟。

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   

第一个脚本的变体,第二个代码使用KeyWait记录按键的长度(D和U状态),并将其存储在log.txt文件中。它有一个限制:不能同时获得多个按键。

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

此第3个脚本是前一个脚本的变体,但它记录了同时的按键。它基于on a script found on AHK Help Docs,并使用SetTimerGetKeyState此最小示例只有2个键(a和b):您需要为键盘的每个键编写相同的内容。我添加条件lasttext2 != text2以使日志文件更小,但是如果你想发送几个{Key Down}(当密钥被保留时),只需删除这个条件。

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