Functions:
getModifierStates
Description
- Returns the list of pressed modifiers in "AHK form" - usable in the Send command
- Optionally returns the list of pressed modifiers in "alphabetical form" - usable in a variable name
Download
getModifierStates.zip
Requirements
None
Functions
getModifierStates(ByRef AlphaForm = "")
Returns a list of the pressed modifiers in "AHK form", and optionally stores them in "alphabetical form" in the passed variable.
Code
Code:
getModifierStates(ByRef AlphaForm = "")
{
AlphaForm := ""
if GetKeyState("LWin", "P") || GetKeyState("RWin", "P")
{
ReturnValue .= "#"
AlphaForm .= "W"
}
if GetKeyState("Ctrl", "P")
{
ReturnValue .= "^"
AlphaForm .= "C"
}
if GetKeyState("Alt", "P")
{
ReturnValue .= "!"
AlphaForm .= "A"
}
if GetKeyState("Shift", "P")
{
ReturnValue .= "+"
AlphaForm .= "S"
}
return ReturnValue
}
ParametersAlphaForm - optionally pass a variable to store the "alphabetical form" of the modifiers (clears the variable's contents first).
The "alphabetical form" is a string consisting of the following characters (in the specified order), omitting the respective characters for unpressed modifiers: "WCAS".
Note: these are the first character in the words Win, Ctrl, Alt, and Shift, respectively.
This string can be used as part of a
variables name (e.g. an array index).
This would allow storing different values depending which modifiers were pressed.
exCode:
getModifierStates(AlphaForm)
myArray[%AlphaForm%] := "Action to perform"
It would also allow getting a different value depending which modifiers were pressed.
Code:
getModifierStates(AlphaForm)
MsgBox, % "Action to perform: " . myArray[%AlphaForm%]
ReturnValueA string consisting of the following characters (in the specified order), omitting the respective characters for unpressed modifiers: "#^!+".
Note: these are AHK modifiers for the Win key, Control Key, Alt key, and Shift key, respectively.
This string can be used in the
Send command.
exCode:
;key to send
Key := F1
;save the modifier states - to use later
ModifierStates := getModifierStates()
sleep, 2000
Send, %ModifierStates%{%Key%}
RemarksBoth the left and the right version of the key will set the state as "pressed" - the function doesn't distinguish between the two.
Both the return value and AlphaForm can be used as quasi-boolean values. The statement
if getModifierStates(...) is "true" if some modifier is pressed, and "false" if no modifiers are pressed. Likewise, the statement
if AlphaForm is "true" if some modifier is pressed, and "false" if no modifiers are pressed.
You can use
inStr to check if a specific modifier is pressed. This works for both the return value and the Alpha form.
ex Code:
ModifierStates := getModifierStates()
if inStr(ModifierStates, "#")
MsgBox, The Win key is in the "pressed" state.
else
MsgBox, The Win key is not pressed.
ex Code:
getModifierStates(AlphaForm)
if inStr(AlphaForm, "W")
MsgBox, The Win key is in the "pressed" state.
else
MsgBox, The Win key is not pressed.
Since the hotkeys will always be returned in the same order, if a given set of modifiers is pressed, the produced string is unique, and can be easily compared. This works for both the return value and the Alpha form.
ex Code:
ModifierStates := getModifierStates()
if (ModifierStates = "#!+")
MsgBox, % "The Win, Alt, and Shift (but not Ctrl) keys are pressed."
else if (ModifierStates = "^!")
MsgBox, % "Only the Control and Alt keys are pressed."
else
MsgBox, % "It is some other combination of modifiers."
ex Code:
getModifierStates(AlphaForm)
if (AlphaForm = "WAS")
MsgBox, % "The Win, Alt, and Shift (but not Ctrl) keys are pressed."
else if (AlphaForm = "CA")
MsgBox, % "Only the Control and Alt keys are pressed."
else
MsgBox, % "It is some other combination of modifiers."
Just to clarify, the function still returns the "AHK form" even when a variable is passed. In the above examples that make use of the Alpha form (and not the AHK form), I opted not to store the return. However, you could, if you wanted to, store both the AHK form and the Alpha form.
exCode:
ModifierStates := getModifierStates(AlphaForm)
How to use
Extract the zip's contents to a
library folder for automatic inclusion - StdLib compliant.
Download getModifierStates function