2017-05-02 6 views
0

誰でも説明できますが、なぜ "エラー"メッセージボックスが表示されますか?`If WinActive`と` GetKeyState`と一緒に

私は、コードは自明であると思います。それが何らかの理由でうまくいかないことがわかったとき、私は「驚いた」。

(開いたメモ帳ウィンドウでF1キーを押すと、「動作します!」というメッセージが表示されますが、代わりに「エラー」メッセージが表示されます)。

私はそれを修正するさまざまな方法、つまりパーセント、変数の割り当て、かっこを試しましたが、現在はまだ動作しません。表情モードリテラル文字列で

#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 

答えて

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 
関連する問題