【问题标题】:Making a key be pressed when another is unpressed (AHK)按下另一个键时按下另一个键 (AHK)
【发布时间】:2019-03-20 09:30:28
【问题描述】:

我正在编写这个脚本。

#Persistent
Random , timerval , 7800 , 8460
SetTimer, PressTheKey,  %timerval%
Return

PressTheKey:
Send, {d}
Return

这是每 8~ 秒按下“d”的基本间隔。有用。问题是,如果按下另一个键,例如鼠标右键,“d”将不会被触发,我将不得不等待剩余的持续时间。

我需要让脚本等待鼠标右键未被按下,或者每 10 毫秒左右运行一次检查以检查鼠标右键是否被按下,如果不是,它可以发送, {d}。

所以,我正在考虑使用 GetKeyState()、KeyWait 或 While 循环来解决这个问题。

#Persistent
Random , timerval , 7800 , 8460
SetTimer, PressTheKey,  %timerval%
Return

GetKeyState, state, RButton
if state = D
KeyWait, RButton

PressTheKey:
Send, {d}
Return

我尝试了这个和其他的,但我无法将其投入使用,我不是编码专家,但我正在努力学习。

有人可以帮我解决这个问题吗?

编辑:按住键一段时间可以解决此问题。

#Persistent
Random , timerval , 7800 , 8460
Random , timerval2 , 180 , 250
SetTimer, PressTheKey,  %timerval%
Return

PressTheKey:
Send, {t down}
Sleep, %timerval2%
Send, {t up}
Return

F1::
Pause
Suspend
return

【问题讨论】:

    标签: key autohotkey auto


    【解决方案1】:

    GetKeyState 在您的 AHK 示例中不起作用,原因是它不在循环内。

    (第一个 Return 会阻止这种情况)

    你可以用这个例子来解决这个问题:

    例子.ahk

    ;#notrayicon
    #SingleInstance force
    #Persistent
    #MaxThreadsPerHotkey 10
    
    Random , timerval , 7800 , 8460
    SetTimer, PressTheKey,  %timerval%
    Return
    
    
    
    PressTheKey:
    GetKeyState, state, RButton
    if state = U
    {
    KeyWait, RButton
    send {esc}
    ;ControlSend, , {esc}, ahk_exe NOTEPAD.EXE ;you can use this codeline for specific Application. 
    }
    
    Send, {d}
    ;ControlSend, , {d}, ahk_exe NOTEPAD.EXE ;you can use this codeline for specific Application. 
    Return
    
    f1::exitapp 
    

    注意 - 如果您单击鼠标右键,则光标将消失在弹出菜单中,您可以使用代码行 send {esc} 修复此问题,或编写代码行将光标移回到那个窗口!

    【讨论】:

    • 感谢您的提醒。不幸的是,这似乎仍然不起作用。也许脚本可以在定时器触发时向上发送 RButton,即使鼠标被物理按下。
    • @Frederico Cintra Torres - 代码行if state = D 代表向下,如果你想使用向上,你可以使用代码行if state = U 注意——如果你用鼠标右键单击然后光标将消失在弹出菜单中,您可以使用代码行send {esc}-我会更改我的答案,希望您会喜欢它
    • 感谢您的尝试,我试过了,但它没有工作,可能只是程序运行的方式。我设法通过简单地按下暂停来修复它,我用它编辑了我的原始帖子。
    【解决方案2】:

    你几乎拥有它。

    #Persistent
    Random , timerval , 7800 , 8460
    SetTimer, PressTheKey,  %timerval%
    Return
    
    PressTheKey:
    KeyWait, RButton, U
    Send, {d}
    Return
    

    我能看到的唯一问题是如果RButton 被按住超过 2 倍的计时器。在这种情况下,我相信它只会触发一个额外的Send, {d},而不是它应该触发的总量。无论如何,根据你所说的情况,这似乎不太可能发生。

    【讨论】:

    • 没有?它做了什么?对我来说,它在释放RButton 后立即发送d,如果它在应该发送d 时被保留,然后恢复计时。
    • 我认为这与程序的工作方式有关。如果是即时点击,它会拒绝新键,但如果我按住键一小段时间,它会起作用。
    • 啊,我明白了。我发现有时SendEvent 的表现比SendInput 好,尤其是在游戏方面。您可能想尝试一下,因为这样就无需按住键一段时间了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2021-02-02
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多