2

I want to set up a AutoHotkey system that uses as few keys as possible to have access to many (e.g. 100) key combinations

For beginners: Here is one good example of how to do the more basic command. Another good method is mentioned here, both of which I am looking to combine and expand upon into a more comprehensive system.

To clarify this question: I have for example 3 keys: a, b, and c. What would be a good way to get the most usage out of only these 3 keys?

To keep it simple, here are 3 examples of ways to use only 3 keys I have thought of:

  • press and hold all 3 for 1 second and release = "MsgBox you pressed A,B,C for one second and released"
  • click all 3 simultaneusly two times = "MsgBox you clicked A,B,C twice"
  • press and hold all 3 for 3 second and release = " MsgBox you pressed A,B,C for three second and released"

The second part of this is how to program AutoHotkey to do this without risking conflicts later when you add a fourth key, etc.

One way I have tried to do this before is to use keystate , and should be straight forward for most when using double click. Here is my work in progress of using this:

CapsLock & a:: ; a hotkey to send a message when you click a two times while 
               ; holding down CapsLock, left Shift, and left Control
GetKeyState, stateLCtrl,    LCtrl
GetKeyState, stateLShift,   LShift
    if stateLCtrl=D
        if stateLShift=D
            if (A_PriorHotkey <> "CapsLock & a" or A_TimeSincePriorHotkey > 500)
            {
                ; Too much time between presses, so this isn't a double-press.
                KeyWait, Esc
                return ; does nothing on single click 
            }
            MsgBox you clicked a two times while holding down 
                   CapsLock, left Shift, and left Control ;output of hotkey 
            return 
        else                
            Return
    else 
        return 
return 

This is just one example, out of many possible ways I can think of how to do this. I think this method is bad for several reasons, so if anyone has a better system that can be easily expanded upon, that would be great!

Community
  • 1
  • 1
Stenemo
  • 611
  • 7
  • 13

1 Answers1

3

I would recommend looking into the #If context. This allows you to specify the keystates that you want without duplicating a lot of "state-checking" code. Here is what your script would look like when using this context.

#If GetKeyState("Shift") and GetKeyState("Ctrl") and GetKeyState("CapsLock")
    *a:: 
        Keywait % A_ThisHotkey
        if (A_PriorHotkey = A_ThisHotkey and A_TimeSincePriorHotkey < 500)
            msgbox, short
        else
            return ; too long
    return
#If 
  • Close context with '#If' if you have universal hotkeys after in your script

  • '*' is needed on the hotkey because Ctrl and Shift are considered "modifiers"

Elliot DeNolf
  • 2,920
  • 1
  • 15
  • 12
  • Ah, had missed #If, this will be much more efficient, and expandable for more letters. Since I have "SetCapslockState, AlwaysOff", I will use #If GetKeyState("Shift") and GetKeyState("Ctrl") CapsLock & a:: – Stenemo Aug 26 '14 at 16:32
  • If this helped you out, feel free to accept this as the answer. – Elliot DeNolf Aug 26 '14 at 16:50
  • Just tested this on my Windows 8 machine (I have the script synced between them, although slightly different how they handle the script (e.g. my Windows 8 setup requires "{" to be on a line with no code, not sure why this is)), and I am unable to get it to work on this setup. Any idea what is causing it? – Stenemo Aug 26 '14 at 17:55
  • I am on Windows 8 myself, and it appears to be working just fine. You copied my script verbatim and double-tapped the `A` key with Shift, Ctrl, and CapsLock down? – Elliot DeNolf Aug 26 '14 at 20:54
  • Yep, it works fine on my Windows 7 computer, but not on my Windows 8. Don't think there are any other differences. – Stenemo Aug 28 '14 at 07:20