【问题标题】:AutoHotKey to accurately log keystrokesAutoHotKey 准确记录击键
【发布时间】:2019-04-07 22:55:15
【问题描述】:

“我正在尝试制作一个 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

测试脚本

$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

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    我制作了这个名为 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 并使用SetTimer 结合GetKeyState这个最小示例只有 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
    

    【讨论】:

    • 您需要查看 Autohotkey 帮助文档中的“输入”。关于方向键,只要添加到 endkeyslist 上即可。关于按键组合,请查看 M 选项(输入)。大约按住键一会儿,考虑结合KeywaitGetKeyState使用。 A_PriorKey 内置变量在构建脚本时也可能很有用(也可以查看一下)。
    • 最终您可能还需要更改 L# 选项(以获得组合键而不是单个键)。
    • 关于箭头键,我刚刚意识到它已经在 endkeyslist 上,并且它正在进行我刚刚在这里进行的测试;
    • 我在顶部更新了我的代码,以便它应该在文本文件中添加时间戳,在该文本文件中按下向上箭头键并释放它。出于某种原因,甚至没有创建文本文件。有什么想法吗?
    • 现在一切正常,谢谢@freestock.tk
    猜你喜欢
    • 2019-08-29
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多