AutoHotkey Community

It is currently May 26th, 2012, 4:39 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: March 30th, 2009, 7:52 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
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 April 12th, 2009, 5:40 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 30th, 2009, 9:16 am 
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.)

8)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 30th, 2009, 9:17 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: maul.esel and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group