使用AutoHotkey记录多个键

时间:2019-04-08 17:18:32

标签: autohotkey

我正在尝试通过AutoHotkey来玩赛车游戏。我使用此脚本将按下的键粘贴到txt中,以便它们自动播放。

我的问题是,现在该脚本只能记录一个键,例如{w down}可以向前驱动。但是为了更好地驾驶游戏,您必须按 w d 向右行驶。

如何更改代码以使其按我想要的方式工作?

$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

1 个答案:

答案 0 :(得分:1)

我建议使用OnMessage()来监视按下(0x100)和按下(0x101)事件消息。以下是指向相关帮助文档的一些链接:OnMessage()List of Windows Messages

下面是一个有效的示例,该示例将在桌面testEE.txt上创建一个文件,该文件的参数与您要记录的参数相似。请注意,这不会记录鼠标的移动或点击(可能,我只是没有那样设置)。按F1开始和停止录制。

f1::
bT := !bT
If bT
{
    MsgBox ,,, Recording started , 1
    KeyBdHook := DllCall(   "SetWindowsHookEx" , "int" , 13  , "uint" , RegisterCallback( "KeyBdProc" ) , "uint" , 0 , "uint" , 0 )
    nStart := A_TickCount
} Else {
    DllCall( "UnhookWindowsHookEx" , "ptr" , KeyBdHook )
    MsgBox ,,, Recording terminated , 1
}
Return

KeyBdProc( nCode , wParam , lParam )
{
    global
    Critical
    If ( wParam = 0x100 || wParam = 0x101 )
    {
        nKey := Format( "{:x}" , NumGet( lParam + 0 , 0 , "int" ) )
        nTime := A_TickCount - nStart , nStart := A_TickCount
        If ( wParam = 0x100 && nKey != nKeyPrev )
        {
            nKeyPrev := nKey
            FileAppend , % nTime . "|" . nKey . " down`n" , %A_Desktop%/testEE.txt
        }
        If ( wParam = 0x101 )
        {
            If ( nKey = nKeyPrev )
                nKeyPrev := ""
            FileAppend , % nTime . "|" . nKey . " up`n" , %A_Desktop%/testEE.txt
    }   }
    Return DllCall( "CallNextHookEx" , "uint" , KeyBdHook , "int" , nCode , "uint" , wParam , "uint" , lParam )
}

这将重播您的按键:

f2::
aData := []
FileRead , sText , %A_Desktop%/testEE.txt
Loop , Parse , sText , `n , `r
{
    nCt := A_Index
    Loop , Parse , A_LoopField , |
        aData[ nCt , A_Index ] := A_LoopField
}
Loop , % aData.Length()
{
    If InStr( aData[ A_Index , 2 ] , "70 " ) ; 0x70 is F1
        Continue
    Sleep , % aData[ A_Index , 1 ]
    Send , % "{vk" . aData[ A_Index , 2 ] . "}"
}
Return

请注意,它还会记录开始和停止记录的热键,因此,如果看到此键,则将其设置为Continue。如果您将热键更改为其他键或组合键,则需要将其设置为忽略该键或组合键。

我发现这些帖子特别有用,仅供参考:
https://autohotkey.com/board/topic/27067-mouse-move-detection/
https://www.autohotkey.com/boards/viewtopic.php?t=11922
How to activate a function everytime the target window becomes active in AutoHotkey
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unhookwindowshookex