Combination of GetKeyStates() -> Derive characters from it? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
drawback
Posts: 34
Joined: 11 Aug 2016, 11:31

Combination of GetKeyStates() -> Derive characters from it?

09 Oct 2016, 04:05

Hi,

Code: Select all

	shiftState := GetKeyState("Shift", "P") ? 1 : 0
	ctrlState  := GetKeyState("Ctrl",  "P") ? 2 : 0
	altState   := GetKeyState("Alt",   "P") ? 4 : 0
	lWinState  := GetKeyState("LWin",  "P") ? 8 : 0
	state      := ctrlState + shiftState + altState + lWinState
Is there a good way to get the corresponding modifier (^, +, ! and #) from the state variable (when used in a different function?
Atm there are only 16 options (0-15) but if I add more keys to it this list will grow extensivly.

Atm I'm doing it one by one and I guess there is a more elegant way...

Code: Select all

    if (state == 0)
        modifier := ""
    else if (state == 1)
        modifier := "+"
    ...
    else if (state == 15)
        modifier := "+^!<#"
Maybe a loop with some kind of bitshifting instead?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Combination of GetKeyStates() -> Derive characters from it?

09 Oct 2016, 05:36

Try this:

Code: Select all

*F12:: Test_Modifiers()



;-------------------------------------------------------------------------------
Test_Modifiers() {
;-------------------------------------------------------------------------------
	shiftState := GetKeyState("Shift", "P") ? 1 : 0
	ctrlState  := GetKeyState("Ctrl",  "P") ? 2 : 0
	altState   := GetKeyState("Alt",   "P") ? 4 : 0
	lWinState  := GetKeyState("LWin",  "P") ? 8 : 0
	state      := ctrlState + shiftState + altState + lWinState

    MsgBox, % state "`n" get_ModifierString(state)
}



;-------------------------------------------------------------------------------
get_ModifierString(state) { ; I use hex here, decimal is also possible
;-------------------------------------------------------------------------------
    Return, (state & 0x1 ? "Shift " : "")
          . (state & 0x2 ? "Ctrl "  : "")
          . (state & 0x4 ? "Alt "   : "")
          . (state & 0x8 ? "LWin "  : "")
}
I hope that helps.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Combination of GetKeyStates() -> Derive characters from it?  Topic is solved

09 Oct 2016, 05:41

alternative:

Code: Select all

;-------------------------------------------------------------------------------
get_ModifierString(state) { ; I use hex here, decimal is also possible
;-------------------------------------------------------------------------------
    Return, (state & 0x1 ? "+"  : "")
          . (state & 0x2 ? "^"  : "")
          . (state & 0x4 ? "!"  : "")
          . (state & 0x8 ? "<#" : "")
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AndreRuas, blad4, freespacing, Google [Bot], Green, john_c, michelxd15, theelf, TLM and 200 guests