 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
animeaime
Joined: 04 Nov 2008 Posts: 1045
|
Posted: Mon Mar 30, 2009 7:52 am Post subject: [Func] getModifierStates - get keystate of ALT/CTRL/SHIFT/.. |
|
|
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
} |
Parameters
AlphaForm - 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.
ex
| Code: | 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%] |
ReturnValue
A 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.
ex
| Code: | ;key to send
Key := F1
;save the modifier states - to use later
ModifierStates := getModifierStates()
sleep, 2000
Send, %ModifierStates%{%Key%} |
Remarks
Both 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.
ex
| Code: | | ModifierStates := getModifierStates(AlphaForm) |
How to use
Extract the zip's contents to a library folder for automatic inclusion - StdLib compliant.
Download getModifierStates function _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts.
Last edited by animeaime on Sun Apr 12, 2009 5:40 pm; edited 3 times in total |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Mar 30, 2009 9:16 am Post subject: [Func] getModifierStates - get keystate of ALT/CTRL/SHIFT/.. |
|
|
Subject line modification proposal ...
Current: | Quote: | | [Func] getModifierStates - returns the pressed modifiers |
Noob friendly (even for foreign noobs) | Quote: | | [Func] getModifierStates - get keystate of ALT/CTRL/SHIFT/.. | ('return' replaced with 'get' for the reason of a restriction regarding the number of chars on the subject line.)
 |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1045
|
Posted: Mon Mar 30, 2009 9:17 am Post subject: |
|
|
Done, thanks. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|