Autohotkey - hotkey 1 do something, key sequence 11 do another thing

1

Im pretty new in AHK, I would like Autohotkey to whenever I press the hotkey "1" in MS Word type HELLOW, but at the same time in the same app (MS Word) I want the key combination "11" (key 1 pressed two times very quickly) to type BYE, is this possible? will AHK type "1HELLOW" when I type "1", will it type "11BYE" when I type "11"? is it possible to do the same script but with F1 instead? I mean F1, and the key sequence F1F1 (F1 pressed twice very quickly)

So far I have tried this

~1::
;400 is the maximum allowed delay (in milliseconds) between presses.
if (A_PriorHotKey = "~1" AND A_TimeSincePriorHotkey < 400)
{
   Msgbox,Double press detected.
}
else if (A_PriorHotKey = "~1" AND A_TimeSincePriorHotkey > 400)
{
    Msgbox,Single press detected.
}

Sleep 0
KeyWait 1
return

But it just work the first time I press the key sequence 11 (1 pressed twice quickly) then it will always recognize only the 1 key, why???

~1::
if (A_PriorHotkey <> "~1" or A_TimeSincePriorHotkey > 400)
{
    ; Too much time between presses, so this isn't a double-press.
    KeyWait, 1
    return
}
MsgBox You double-pressed the 1 key.
return

this doesn't help to get the two hotkeys, (1 and 11) either.

Thanks Advanced.

litu16

Posted 2016-11-21T00:31:37.503

Reputation: 143

Looking into this quickly, it seems variables are of local scope, so they die when the script ends. This one might help, through: special variable A_PriorKey (or A_PriorHotKey or A_ThisHotKey, located nearby in documentation).

– TOOGAM – 2016-11-21T04:06:11.070

Answers

1

It works best by using SetTimer:

    ; The following hotkeys work only if MS-WORD is the active window:
#If WinActive("ahk_exe WINWORD.EXE")    ; (1)

    1::
    if 1_presses > 0
    {
        1_presses += 1
        SetTimer Key1, 300
        return
    }
    1_presses = 1
    SetTimer Key1, 300
    return

    Key1:
    SetTimer Key1, off
    if 1_presses = 2
      SendInput, BYE
    else
      SendInput, HELLOW
    1_presses = 0
    return

    F2:: MsgBox, You pressed F2 in MS-Word


    ; The following hotkeys work only if NOTEPAD is the active window:
#If WinActive("ahk_exe NOTEPAD.EXE") 

    1:: Send 2

    F2:: MsgBox, You pressed F2 in NOTEPAD


#If ; turn off context sensitivity (= end of context-sensitive hotkeys)


; The following hotkeys work only if MS-WORD or NOTEPAD is NOT the active window (because they are already defined in those programs):

1:: Send 3

F2:: MsgBox, You pressed F2 while  MS-WORD or NOTEPAD is NOT the active window


; The following hotkeys work in all windows (incl. MS-WORD and NOTEPAD because they are NOT defined in those programs)

F3:: MsgBox, You pressed F3

Esc:: ExitApp

https://autohotkey.com/docs/commands/SetTimer.htm#Examples (Example #3)

(1) Like the #IfWin directives, #If creates context-sensitive hotkeys and hotstrings and is positional: it affects all hotkeys and hotstrings physically beneath it in the script.

user3419297

Posted 2016-11-21T00:31:37.503

Reputation: 2 055

Hi user3419297 thanks for your support, It worked like a charm even with the F1 key, I just wonder lets say I have lots of scripts that run only in MSword, (like 1pressed twice, 2pressed twice, 3 pressed twice, and other different like Ctrl Alt P) I just put #If WinActive("ahk_exe WINWORD.EXE") ; (1) at the very beginning of all of them, and only put #If ; turn off context sensitivity at the end of all of them, right? – litu16 – 2016-11-21T12:02:09.907

Answer to your first question: You only need one script for defining hotkeys or hotstrings in MSword and in any other program. I edited my answer to help you better understand, how context-sensitive hotkeys work.Please, delete your second question and add it after this comment in case my answer doesn't satisfy you. – user3419297 – 2016-11-21T13:14:39.997

Thanks user3419297, I got it, but what if in MSword I have script that is triggered when 1 ` (the key above tab and 1) is pressed in sequence, and another when just the 1 is pressed, both scripts only run in MSword, I mean, if I press the first key sequence will it trigger the second script also??? do they interfere with each other? Thanks Advanced. – litu16 – 2016-11-21T14:13:17.057

btw I have also some little question http://superuser.com/questions/1148186/autohotkey-set-a-hotkey-to-toggle-disabled-enabled-a-hole-autohotkey-script Thanks agian

– litu16 – 2016-11-21T14:54:48.730

How do you define this sequence in the script? Add the code in your question or (better) make a new question in Super User. – user3419297 – 2016-11-21T14:58:03.307

Hi, thanks user 3419297 I made the new questions http://superuser.com/questions/1148238/autohotkey-1-key-sequence please could you check it? Thanks Advanced.

– litu16 – 2016-11-21T16:45:02.890