`If​​ WinActive` 与 `GetKeyState` 一起

`If WinActive` together with `GetKeyState`

谁能解释一下,为什么我会收到 "Error" 消息框?

我认为,代码是不言自明的。当我发现它出于某种原因不起作用时,我才 "surprised"。

(当您在打开的记事本 window 中按 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

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

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