How to set same shortcut (^q) to send one command when single pressing, and another when holding ^ and pressing q twice? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

How to set same shortcut (^q) to send one command when single pressing, and another when holding ^ and pressing q twice?

20 Jun 2019, 21:12

Hi,

I'd appreciate help.

How to set the same shortcut ^q to send one command when single pressing, and another when holding ^ and pressing q twice?

Thank you very much!
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: How to set same shortcut (^q) to send one command when single pressing, and another when holding ^ and pressing q tw  Topic is solved

20 Jun 2019, 23:10

Try this.
If you press Ctrl + Q twice within 300 milliseconds, the MsgBox will show different text.
But if you need the single press mode, than you will need to press Ctrl + Q, release it, and wait for 300 milliseconds.

Code: Select all

#MaxThreadsPerHotkey, 2

KeyPress := 0, running := False

^q::
    KeyPress++

    If (running = True) {
        Return
    }

    running := True

    While (A_TimeSinceThisHotkey < 300) {
        If (KeyPress > 1) {
            MsgBox, Two times

            KeyPress := 0, running := False
            Return
        }
    }

    MsgBox, One time

    KeyPress := 0, running := False
Return
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: How to set same shortcut (^q) to send one command when single pressing, and another when holding ^ and pressing q tw

21 Jun 2019, 12:43

Many thanks you all for the replies.
I'm going to explore Morse(), and Ridwan's script also works perfectly, fitting all I needed!

I further question if I may. Regarding the heading of the script:

Code: Select all

#MaxThreadsPerHotkey, 2
KeyPress := 0, running := False
Since I'm going to use this script to run several commands stored within a single .ahk file, should I repeat that heading for each individual script within the file, or could I insert the heading once, just in the beginning of the script?

Thank you very much!
Ridwan
Posts: 144
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: How to set same shortcut (^q) to send one command when single pressing, and another when holding ^ and pressing q tw

21 Jun 2019, 18:22

Code: Select all

#MaxThreadsPerHotkey, 2
Just need to declared once for each file.

Code: Select all

KeyPress := 0, running := False
also need to declared once at the beginning, but one for each Hotkey. If you need more, like Ctrl + K than you will need to add different variable, for example:

Code: Select all

KeyPress_K := 0, running_K := False
and repeat it after each key press (single or twice) to reset it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mamo691, MrDoge, ReyAHK and 237 guests