2

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.

Community
  • 1
  • 1
mr_T
  • 2,571
  • 3
  • 22
  • 39
  • Why didn't the suggestions in the linked question do the trick? Please post your code and specify what didn't work. – MCL Sep 03 '14 at 08:19
  • I have added the specific code that I have tried. – mr_T Sep 03 '14 at 08:53
  • 1
    Have a look at the [Input docs](http://ahkscript.org/docs/commands/Input.htm). 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 Sep 03 '14 at 09:54

1 Answers1

1

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!

vasili111
  • 6,032
  • 10
  • 50
  • 80
  • Did you know that `return` after `ExitApp` is unreachable code? Also, I don't think this exactly matches OP's requirements, since in your examples, the keys have to be pressed simultaneously. In contrast, I believe OP wants to be able to exit by typing a combination like `CTRL`, `ESC`, `CTRL` one after another. – MCL Sep 04 '14 at 06:40
  • @MCL Yes, you are right about `return` after `ExitApp`. I did not paid attention to that, I just always writing `return` at the end of hotkey. – vasili111 Sep 05 '14 at 11:51
  • @MCL OP said: `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"`. So Understand that "Esc and then LCtrl" is ok for him. – vasili111 Sep 05 '14 at 11:54
  • @MCL Added also version 3. – vasili111 Sep 05 '14 at 12:00
  • @T_at'Ghent Added also version 3. – vasili111 Sep 05 '14 at 12:01
  • A permanent loop in order to catch a key sequence sounds rather rash. Also, after `ESC` was pressed, the script will "wait" indefinitely until `LCtrl` is pressed and then exit, without taking the time between key pressed into consideration. In other words, if you press `ESC` and 10 minutes later `LCtrl`, the script will exit anyway, which can't be what you want. – MCL Sep 05 '14 at 13:17
  • @MCL Please check version 3 again. I corrected version 3. I though previous version was exactly what OP asked for but newer version 3 is more logically right and I think he will like it more then he asked for. – vasili111 Sep 05 '14 at 13:40