问题
What is wrong with this?
if A_OSVersion = WIN_7 or A_OSVersion = WIN_XP
{
MsgBox, Win7 or XP
} else {
if WinActive("ahk_exe Explorer") {
!f::!h
}
}
The goal is to replace Alt+F with Alt+H in Explorer when I'm running Windows 8, 8.1, or 10, and I am testing on a Windows 7 machine, so the code shouldn't run at all, but the hotkey does and the MsgBox
doesn't. Do I need to reorder things for it to work? The syntax looks right to me, but I've been having a hard time with syntax for AHKscript.
Kind comments are appreciated!
回答1:
This
!f::!h
isn't a command, it's a key remap and thus, implicitly contains a return
. You cannot use it within executable context. For details, see http://ahkscript.org/docs/misc/Remap.htm#Remarks somewhere below.
You wouldn't wanna have a return somewhere in between the lines which should be executed, would you.
(this was mostly a copy of my answer here)
For context-sensitive hotkeys, use the preprocessor directives:
#if (A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP") and not winActive("ahk_exe Explorer")
!f::!h
#if
回答2:
#if winActive("ahk_exe Explorer") and !(A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP")
!f::!h
#if
To do it within normal script flow you will need to used the Hotkey Command but then it will not be a remap but a hotkey that sends another set of keys...
来源:https://stackoverflow.com/questions/33509021/autohotkey-if-statement-not-working