问题
I am making an AutoHotKey script so that I have media keys on my media key-less keyboard.
All of the shortcuts are working but i want to make one that will set the volume to max, unless it is already, in which case it would set it to 10.
Here is my script as it is:
SoundGet, master_volume
^!Down::SendInput {Media_Play_Pause}
^!Left::SendInput {Media_Prev}
^!Right::SendInput {Media_Next}
^!Up::SendInput {Media_Stop}
^!+Down::SendInput {Volume_Mute}
^!+Left::SendInput {Volume_Down}
^!+Right::SendInput {Volume_Up}
^!+Up::if (%master_volume% = 100) {
SoundSet, 10
} else {
SoundSet, 100
}
But I keep getting the error "Error: The following variable name contains an illegal character: "100.000000"" (the 100.000000 is what my volume is set to, e.g. if it's set to half maximum, it'll read 50.000000) and it highlights "if (%master_volume% = 100)" line ...
I'm pretty new to AutoHotKey, but I have read through a lot of (what I thought were) relevant docs and can't figure it out
回答1:
this should work
^!+Up::
if (master_volume = 100) {
SoundSet, 10
} else {
SoundSet, 100
}
return
remove %'s around "master_volume"
回答2:
The error comes from you using the `%master_volume% the way you are. Consider:
When you do this:
somevariable = george
the value of somevariable
is set to "george"
But when you do this:
%somevariable% = george
the variable name is set to "george"
That means when you do this:
%master_volume% = 100
You are taking the value of variable_name
and turning it into a variable, too. Effectively, you are trying to create a variable called 100.000000
and assign it the value of 100
.
Since the value of master_volume
is 100.000000
it is illegal because you can't use .
in a variable name.
However, if you used SetFormat, "D", 0
, then you would force your decimals not to have any floating point (no .000000
) and then you would be able to create a variable out of 100
or 75
or whatever the value of master_volume
happens to be.
However, you probably don't want it to work that way. You should use the simple answer proposed by alpha bravo, and just use:
master_volume = 100
来源:https://stackoverflow.com/questions/24023906/autohotkey-error-variable-name-contains-an-illegal-character-100-000000