Execute action 1 if key pressed, execute action 2 if key held

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aybe
Posts: 3
Joined: 17 Jun 2018, 17:43

Execute action 1 if key pressed, execute action 2 if key held

17 Jun 2018, 17:49

I would like to achieve the following behavior:

- if key is pressed, execute action 1
- if key is held for more than 2 seconds, execute action 2 instead

I have the first part of the code working but I can't figure out the second part:

Code: Select all

Send, #1
return
Basically on second part, I would like to press Win+Alt+1 instead of Win+1.

Thanks for your help :D
aybe
Posts: 3
Joined: 17 Jun 2018, 17:43

Re: Execute action 1 if key pressed, execute action 2 if key held

17 Jun 2018, 23:19

Okay I've made some progress but it's still not right !

Code: Select all

#SingleInstance force
$a::
aDown:=A_TickCount
Keywait a, T 0.5 ; timeout
Duration:=(A_TickCount-aDown)
If (Duration<300)
Send #1
Else
Send #!1
Return
So if I press 'a' quickly, it starts or brings front 1st app in taskbar (wanted)

But when I hold it, it does show pinned tasks (wanted), but when I release key, the window is activated again (unwanted).

The other thing I can't figure out is how to run the script only once.

Thanks for your help :mrgreen:
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Execute action 1 if key pressed, execute action 2 if key held

18 Jun 2018, 04:55

Try this:

Code: Select all

$a:: 
    KeyWait, a, T2             
    If ErrorLevel ; timed out
        Send, #!1
    Else
        Send, #1
Return
I hope that helps. untested.
aybe
Posts: 3
Joined: 17 Jun 2018, 17:43

Re: Execute action 1 if key pressed, execute action 2 if key held

18 Jun 2018, 17:39

Sorry but it doesn't work, when the second action gets executed, the first gets executed right after !

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 226 guests