You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于按键顺序用相同按键设置两个独特热键?

Solution for Order-Dependent Modifier Hotkeys

The core issue with your original code is that GetKeyState only checks if a key is pressed, not the order in which it was pressed. When both LShift and Space are held, both conditional blocks become true, leading to conflicts. To distinguish between the two sequences, we need to explicitly track the order of modifier key presses.

Here's a working implementation:

; Initialize state variables to track modifier order
shiftPressedFirst := false
spacePressedFirst := false

; Track when LShift is pressed first (before Space)
~LShift::
if !GetKeyState("Space", "P")
    shiftPressedFirst := true
return

; Track when Space is pressed first (before LShift)
~Space::
if !GetKeyState("LShift", "P")
    spacePressedFirst := true
return

; Handle F key press with both modifiers held
~F::
if GetKeyState("LShift", "P") && GetKeyState("Space", "P") {
    if shiftPressedFirst {
        Tooltip, You pressed LShift + Space + F
    } else if spacePressedFirst {
        Tooltip, You pressed Space + LShift + F
    }
    SetTimer, Tooltip, -1500 ; Auto-hide tooltip after 1.5 seconds
}
return

; Reset state variables when either modifier is released
~LShift Up::
~Space Up::
shiftPressedFirst := false
spacePressedFirst := false
return

Key Details:

  • State Tracking: Variables shiftPressedFirst and spacePressedFirst are set only if the other modifier isn't already held when the key is pressed.
  • Normal Key Functionality: The ~ prefix ensures that LShift, Space, and F still behave normally when not used in the combination (e.g., Shift for capitalization, Space for spacing).
  • Tooltip Auto-Hide: The SetTimer command hides the tooltip after 1.5 seconds to avoid clutter.
  • State Reset: When either modifier is released, the tracking variables are reset to false, ensuring fresh state for the next key sequence.

Edge Case Note:

If both LShift and Space are pressed nearly simultaneously, neither tracking variable will be set. This is a rare scenario, but you can add a fallback action if needed by adding an else clause in the F key handler.

内容的提问来源于stack exchange,提问作者Ralf_Reddings

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:05:55