【问题标题】:`If WinActive` together with `GetKeyState``If​​ WinActive` 和 `GetKeyState`
【发布时间】:2017-05-02 16:05:23
【问题描述】:

谁能解释一下,为什么我会收到“错误”消息框?

我认为,代码是不言自明的。当我发现它由于某种原因不起作用时,我感到“惊讶”。

(当您在打开的记事本窗口中按 F1 时,您应该会看到“It works!”消息。相反,我收到“Error”消息)。

我尝试了不同的方法来修复它,即百分比、变量赋值、括号,但目前它仍然不起作用。

#SingleInstance, Force
SetTitleMatchMode, 2

f1::
+f1::
GetKeyState, shift_state, Shift
msgbox, %shift_state%
if (WinActive("Notepad") and shift_state = D)
    msgbox, It works! By the way, the Shift key is pressed.
else if (WinActive("Notepad") and shift_state = U)
    msgbox, It works! The Shift key is not pressed.
else
    msgbox, error
return

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    在表达式模式中,文字字符串必须用双引号引起来以区别于变量。

    https://autohotkey.com/docs/Variables.htm#Expressions

    表情模式:

    #SingleInstance, Force 
    SetTitleMatchMode, 2
    
    f1::
    +f1::
    GetKeyState, shift_state, Shift
        msgbox, %shift_state%
    if (WinActive("Notepad") and shift_state = "D")
        msgbox, It works! By the way, the Shift key is pressed.
    else if (WinActive("Notepad") and shift_state = "U")
        msgbox, It works! The Shift key is not pressed.
    else
        msgbox, error
    return
    

    传统模式:

    #SingleInstance, Force 
    SetTitleMatchMode, 2
    
    f1::
    +f1::
    GetKeyState, shift_state, Shift
        msgbox, %shift_state%
    IfWinActive Notepad
    {
        If shift_state = D
            msgbox, It works! By the way, the Shift key is pressed.
        else If shift_state = U
            msgbox, It works! The Shift key is not pressed.
    }
    else
        msgbox, error
    return
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2014-08-21
      • 2011-09-13
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      相关资源
      最近更新 更多