简体   繁体   English

AutoHotkey 导致控制键卡住

[英]AutoHotkey causing control key to get stuck

I have several situations when my control key gets stuck, and it happens only when I have AutoHotkey running.当我的控制键被卡住时,我有几种情况,只有当我运行 AutoHotkey 时才会发生。 This happens with multiple different modifier keys including the control (^), windows (#), and alt (.) keys.这发生在多个不同的修饰键上,包括控制 (^)、窗口 (#) 和 alt (.) 键。

Similar problems have been posted several times before: 1 , 2 , 3 .类似的问题之前几次1、2、3 Some solutions exists, and the one suggested here partially helped me (decreased the frequency of the problem), but the control key still gets stuck occasionally.存在一些解决方案, 这里建议的解决方案部分帮助了我(降低了问题的频率),但控制键仍然偶尔会卡住。 Things I have tried include #InstallKeybdHook .我尝试过的事情包括#InstallKeybdHook

I have two questions:我有两个问题:

  1. Is it possible to prevent this problem?是否可以防止此问题?
  2. Is there a good way to have AutoHotkey monitor when keys are stuck (eg automatically notice when keys have been held for >10s) and fix this as soon as it happens?有没有什么好方法可以让 AutoHotkey 在键被卡住时进行监控(例如,当键被按住超过 10 秒时自动通知)并在它发生时尽快修复它?

I have tried everything suggested above, and created my own version of a StuckKeyUp function (as suggested here) :我已经尝试了上面建议的所有方法,并创建了我自己的 StuckKeyUp 函数版本(如此处建议)

StuckKeyUp(){
sleep 300 
send {<# up} 
send {># up} 
send {# up} 
send {+ up} 
send {<+ up} 
send {! up} 
send {<! up} 
send {>! up} 
send {^<^^>! up} 
send {^<^>! up} 
send {^ up} 
send {Ctrl down} 
send {Ctrl up}

Send {§ up}         
Send {Shift Up}
Send {LShift Up}
Send {RShift Up}
Send {Alt Up}
Send {LAlt Up}
Send {RAlt Up}
Send {Control Up}
Send {LControl Up}  
Send {<^ down}      
Send {<^ Up}        ; solves some issues, but not all
Send {>^ down}      
Send {>^ Up}        
Send {RControl Up}
Send {LControl Up}
Send {LWin Up}
Send {RWin Up}
sleep 100 
; reload, ; Avoid - Reloading AutoHotkey File causes functions depending on this function to break
return 
}

Among other debugging methods you can try this:在其他调试方法中,您可以试试这个:

Put this code at specific positions in your script (within hotkey definitions that send the control key or in a timer)将此代码放在脚本中的特定位置(在发送控制键或计时器的热键定义中)

If GetKeyState("Ctrl")           ; If the OS believes the key to be in (logical state),
{
    If !GetKeyState("Ctrl","P")  ; but  the user isn't physically holding it down (physical state)
    {
        Send {Blind}{Ctrl Up}
        MsgBox,,, Ctrl released
        KeyHistory
    }
}

and look in the KeyHistory (after closing the message box) in which situations the key gets stuck.并查看 KeyHistory(关闭消息框后)在哪些情况下键卡住了。

I also had this problem (with only Ctrl ever getting stuck), and the fix for me was to use #MenuMaskKey at the top of the script:我也遇到了这个问题(只有 Ctrl 卡住了),我的解决方法是在脚本顶部使用#MenuMaskKey:

;; Avoid Ctrl getting stuck in down state, even when not physically pressed:
#MenuMaskKey vkFF

Background info from https://www.autohotkey.com/docs/commands/_MenuMaskKey.htm来自https://www.autohotkey.com/docs/commands/_MenuMaskKey.htm的背景信息

The mask key is sent automatically to prevent the Start menu or the active window's menu bar from activating at unexpected times.屏蔽键会自动发送,以防止“开始”菜单或活动窗口的菜单栏在意外时间激活。

The default mask key is Ctrl.默认屏蔽键是 Ctrl。

... ...

If the system was to detect only a Win or Alt keydown and keyup with no intervening keypress, it would usually activate a menu.如果系统只检测到 Win 或 Alt 键按下和键弹起而没有中间按键,它通常会激活一个菜单。 To prevent this, the keyboard or mouse hook may automatically send the mask key.为了防止这种情况,键盘或鼠标钩子可能会自动发送屏蔽键。

While user3419297's answer is very good, I think the blind keyup call is not what fixes the problem.虽然 user3419297 的回答非常好,但我认为blind keyup调用并不能解决问题。

It appears that the KeyHistory command causes the keys to be released.似乎是KeyHistory命令导致密钥被释放。

When I used a version of user3419297's script without KeyHistory , it never worked for me.当我使用不带KeyHistory的 user3419297 脚本版本时,它对我不起作用。 In contrast, invoking KeyHistory alone has been sufficient to resolve the problem for me every time it has occured.相比之下,每次发生问题时,单独调用KeyHistory就足以为我解决问题。

I added #InstallKeybdHook and all was well.我添加了#InstallKeybdHook,一切都很好。 See below for details.详情见下文。

https://www.autohotkey.com/docs/commands/_HotkeyModifierTimeout.htm https://www.autohotkey.com/docs/commands/_HotkeyModifierTimeout.htm

This problem suddenly started occurring in a script that had been working without a hitch for years and to which I had made no changes.这个问题突然开始出现在一个脚本中,这个脚本多年来一直没有任何问题,我也没有对其进行任何更改。 FWIW it started after a Windows update that seems to have slightly slowed down both AHK and python scripts that send keystrokes to applications. FWIW 它是在 Windows 更新之后启动的,该更新似乎稍微减慢了向应用程序发送击键的 AHK 和 python 脚本。 Which everyone seems to say can't happen but I know what I see.每个人似乎都说这不可能发生,但我知道我看到了什么。 Anyhow, I tried everything I could find and nothing worked until I put this at the start of my script:无论如何,我尝试了所有我能找到的东西,但直到我把它放在我的脚本开头之前,没有任何效果:

^T:: ;Script starts here
    Sleep, 500
    Send {LCtrl Up}
    ;Script continues and Ctrl is no longer pressed :)

Sorry I don't have a precise technical explanation for why this works.抱歉,我没有准确的技术解释来解释为什么会这样。 I'm guessing that I'm not letting go of the Ctrl key fast enough when I press my hotkey (in this case Ctrl-T), but that it somehow hasn't quite registered with AHK.我猜想当我按下热键(在本例中为 Ctrl-T)时,我松开 Ctrl 键的速度不够快,但不知何故它还没有完全注册到 AHK。 Whatever the case, that little pause before sending the key up seems to work every time.不管怎样,在发送密钥之前的那个小停顿似乎每次都能奏效。 The 500 ms was arbitrary; 500 毫秒是任意的; it works and I haven't bothered trying to make it any shorter.它有效,我没有费心尝试让它更短。 No more pressing Ctrl after every script, yay!在每个脚本之后不再按 Ctrl,耶!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM