【问题标题】:(AHK) If statement with GetKeyState not working?(AHK) 如果使用 GetKeyState 的语句不起作用?
【发布时间】:2013-04-28 05:42:24
【问题描述】:

所以我正在尝试创建一个脚本,该脚本将在按住鼠标中键的同时左右滚动。但是,无论是否按住鼠标中键,左右滚动都会滚动。它总是执行。我需要帮助来解决这个问题。

(我确实注意到在第 21 行有太多空间,忽略它) 代码:

; Hold the scroll wheel and scroll to scroll horizontally
; Scroll up = left, scroll down = right

#NoEnv
;#InstallMouseHook

#HotkeyInterval 1
#MaxHotkeysPerInterval 1000000 ; Prevents the popup when scrolling too fast

GetKeyState, ScrollState, MButton

if(ScrollState = U)
{
        ;return
}
else if(ScrollState = D)
{
        WheelUp::Send {WheelLeft}
        return

        WheelDown::     Send {WheelRight}
        return
}
return

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    此方法保留所有正常的中键单击功能,但在按下时仅切换变量state。每当使用 Wheelup 或 Wheeldown 时,都会检查此变量。

    ~Mbutton::
        state := 1
    Return
    
    ~Mbutton up::
        state := 0
    Return
    
    WheelUp:: Send % (state) ? "{WheelLeft}" : "{WheelUp}"
    WheelDown:: Send % (state) ? "{WheelRight}" : "{WheelDown}"
    
    /*
    The ternary operators are short for:
    If state = 1
        Send {WheelLeft}
    else
        Send {WheelUp}
    */
    

    【讨论】:

    • 谢谢。如果您能解释最后两行的含义(尤其是 % 和 ? 符号),那就太好了。
    • 它使用所谓的三元运算符,它是 if/then/else 的缩写。所以对于 Wheelup,如果 state = 1 则发送结果 {WheelLeft} 否则发送 {WheelUp}。为了清楚起见,我添加了我的答案。
    【解决方案2】:

    由双冒号定义的热键不受常规if 语句的控制。要使热键上下文相关,您需要使用#If(或#IfWinActive#IfWinExist)。文档中的一个示例(上下文相关的热键部分):

    #If MouseIsOver("ahk_class Shell_TrayWnd")
    WheelUp::Send {Volume_Up}     ; Wheel over taskbar: increase/decrease volume.
    WheelDown::Send {Volume_Down} ; 
    

    您也可以将常规的if 逻辑放入热键(这是热键提示和备注部分的示例):

    Joy2::
    if not GetKeyState("Control")  ; Neither the left nor right Control key is down.
        return  ; i.e. Do nothing.
    MsgBox You pressed the first joystick's second button while holding down the Control key.
    return
    

    通过#If 的上下文敏感性旨在控制您的热键在哪个应用程序中处于活动状态。热键定义中的常规if 逻辑适用于任意条件。您正在尝试做的事情适合后者。

    在许多情况下,两者都做很有用。例如,如果您希望您的左/右行为仅在浏览器中而不在 Microsoft Word 中,您将使用#If 将热键活动限制在浏览器中,然后在热键定义中使用if GetKeyState(...) 检查是否滚动按钮被按下。

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2013-06-20
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多