2
votes

In order to end my custom keyboard mapping for coding, I have placed the following line at the bottom of my script:

Esc::ExitApp

However, a single keystroke (especially the Esc) happens too much for other reasons. Therefore, I would prefer a sequence of single keystrokes (not holding down any key) to exit the Autohotkey mapping, such as "Esc and then LCtrl" or "Esc, LCtrl and then again Esc"

I have tried the idea from AutoHotKey key SEQUENCE, not just single-key hotkey :

Esc::
Input Key, L1
if Key=LCtrl 
ExitApp  
return

But it doesn't seem to do the trick: on pressing Esc and then LCtrl, the mapping doesn't stop. (Also, if I press Esc and then another key (not LCtrl) that next keystroke is ignored, something I would preferably not have.

1
Why didn't the suggestions in the linked question do the trick? Please post your code and specify what didn't work.MCL
I have added the specific code that I have tried.mr_T
Have a look at the Input docs. In order to catch non-character keys, you can define EndKeys. Anyway, why don't you just use a native hotkey like ^ESC or ^+ESC? It's just as safe when it comes to accidental triggering of your routine and will save you lots of scripting trouble.MCL

1 Answers

1
votes

Version 1a:

LCtrl & Esc::
ExitApp
return

You can add this code anywhere in your script. Press LCtrl and after Esc for script to exit.

Version 1b:

Esc & LCtrl::
ExitApp
return

You can add this code anywhere in your script. Press Esc and after LCtrl for script to exit.

You can add both Version 1a and Version 1b. In that case, when you press LCtrl and after Esc OR Esc and after LCtrl script will exit.


Version 2 (it uses different method, use it if for some reason you don't like Version 1a or/and Version 1b):

Loop
{
    a := GetKeyState("Esc")
    if (a=1)
    {
        b := GetKeyState("LCtrl")

        if (b=1)
        {
            ExitApp
        }

    }
}

That code should be launch after your other code will remap all keys you need. Press LCtrl and after Esc OR Esc and after LCtrl for script to exit.


EDIT:

Version 3:

Loop
{
    a := GetKeyState("Esc")
    if (a=1)
    {
        Loop,60 ;number of 50 milliseconds script will wait to CTRL press. Example 60 means 60*50=3000, so script will wait 3000 milliseconds (1sec=1000 milliseconds) for CTRL press. After that time you have to press Esc again.
        {
            b := GetKeyState("LCtrl")

            if (b=1)
            {
                ExitApp
            }
            Sleep, 50
        }

    }
}

That code should be launch after your other code will remap all keys you need. Press Esc (now you can release Esc ) and within 3000 milliseconds (1sec=1000 milliseconds) press LCtrl. After that time you have to press Esc again for script to exit. If you want to modify time after which you need to press Esc again read the comment in the code.

Also, always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!