Long press to perform more than one action Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sulphuricsaliva
Posts: 48
Joined: 11 Sep 2018, 12:37

Long press to perform more than one action

21 Feb 2022, 09:03

Hello, feeling kind of dumb to ask this, I'm sure It's simple but any help would be much appreciated.

So I want to long press the enter key for 1 second to send esc, and 1.5 seconds to perform alt+f4 and so on. I can make the esc work but not the alt+f4.
This is what I have:

Code: Select all

$*enter::
Send {Blind}{enter}

KeyWait, enter, T1.0
If ErrorLevel 
        { Send {Esc}
	KeyWait, enter
}

KeyWait, enter, T1.5
If ErrorLevel { 
Send !{f4}
	KeyWait, enter

}
return
[Mod edit: [code][/code] tags added.]

I've managed to do this once but can't remember how to do it and It's taking me a while to get there, please help. Cheers.
Last edited by gregster on 21 Feb 2022, 09:11, edited 1 time in total.
Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Long press to perform more than one action  Topic is solved

21 Feb 2022, 13:20

Hallo,
try:

Code: Select all

$*enter::
Send {Blind}{enter}
KeyWait, enter, T1.0 ;press the enter key for 1 second
If ErrorLevel
	Send {Esc}
KeyWait, enter, T.5 ;press the enter key for 1 + .5 second
If ErrorLevel
	Send !{f4}
KeyWait, enter
return
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Long press to perform more than one action

21 Feb 2022, 14:05

Also you can add a ToolTip to Rohwedder's solution. I don't recommend using Soundbeeps here since they eat some time.

Code: Select all

$*enter::
Send {Blind}{enter}
SetTimer, Tip, 100
KeyWait, enter, T1.0 ;press the enter key for 1 second
If ErrorLevel
	Send {Esc}
KeyWait, enter, T.5 ;press the enter key for 1 + .5 second
If ErrorLevel
	Send !{f4}
KeyWait, enter
SetTimer, Tip, Off
ToolTip
return

Tip:
switch time := Round(A_TimeSinceThisHotkey/1000, 1) {
	case 1: ToolTip, ESC ESC ESC`nESC ESC ESC`nESC ESC ESC
	case 1.5: ToolTip, Alt+F4 Alt+F4 Alt+F4`nAlt+F4 Alt+F4 Alt+F4`nAlt+F4 Alt+F4 Alt+F4
	Default: ToolTip, % time  " seconds"
}
return
If you don't want to send {Esc} in case of held longer than 1.5 sec and send only Alt+F4 in that case, you may delay sending of Esc until you release the button and calculate the output:

Code: Select all

$*enter::
Send {Blind}{enter}
SetTimer, Tip, 100
KeyWait, enter, T1.0 ;press the enter key for 1 second
If ErrorLevel
{	KeyWait, enter, T.5 ;press the enter key for 1 + .5 second
	If ErrorLevel
	{	Send !{f4}
		KeyWait, enter
	}
	else Send {Esc}
}
SetTimer, Tip, Off
ToolTip
return

Tip:
switch time := Round(A_TimeSinceThisHotkey/1000, 1) {
	case 1: ToolTip, ESC ESC ESC`nESC ESC ESC`nESC ESC ESC
	case 1.5: ToolTip, Alt+F4 Alt+F4 Alt+F4`nAlt+F4 Alt+F4 Alt+F4`nAlt+F4 Alt+F4 Alt+F4
	Default: ToolTip, % time  " seconds"
}
return
P.S.
Add code tags, please, otherwise you make moderators to add them in your post.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
sulphuricsaliva
Posts: 48
Joined: 11 Sep 2018, 12:37

Re: Long press to perform more than one action

22 Feb 2022, 03:08

Awesome, just what I needed, thank you both so much for your help. :dance: :dance:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk and 350 guests