single press, double press and long press

Get help with using AutoHotkey and its commands and hotkeys
User avatar
schwarzgrau
Posts: 23
Joined: 01 Nov 2015, 11:16
Location: Germany
Contact:

single press, double press and long press

01 Nov 2015, 11:29

I recently switched from OS X to Windows and so from Controllermate to AutoHotkey, but it's hard for me to write my script the way I would like it.
I try to write a script letting a single button perform three actions, one on single press, one on double press and the last one if you press the button for some time.
I found some scripts in the old forum and in the help, but I can't figure out how to combine them.

This script can be used for double pressing

Code: Select all

~z::
    KeyWait, z			; wait for z to be released
    KeyWait, z, D T0.2		; and pressed again within 0.2 seconds
    if ErrorLevel 			; timed-out (only a single press)
        Send single
    Else
        Send double
Return
This one for long press

Code: Select all

$z::                                              		; Long press (> 0.5 sec) on * substitutes the dot multiply
    KeyWait, z, T0.5                               	; Wait no more than 0.5 sec for key release (also suppress auto-repeat)
    If ErrorLevel {                                    	; timeout, so long press
        Send longpress                               
        KeyWait, z
    } Else                                                	; otherwise...
        Send single                               
Return
And this is my attemp to combine these two scripts into one

Code: Select all

$z::                                               

    KeyWait, z, T0.5   

    If ErrorLevel {                                     
        Send longpress                                 
        KeyWait, z
    } Else  
        KeyWait, z, D T0.2	
    	if ErrorLevel 
        	Send single
    	Else
        	Send double                                                                     
Return
Single press and double press works like they should, just the longpress sends "longpressdouble".
I would be very happy for every tip, how to fix this.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: single press, double press and long press

01 Nov 2015, 11:45

It's a matter of brackets. Wrap your 5 lines after } Else inside of a block as well. RIght now, only your KeyWait, z, D T0.2 line is associated with the Else. So what happens is when Send longpress happens, and then you use KeyWait, z, the KeyWait will wait for the Z to be released. Now, ErrorLevel has been set back to zero. The KeyWait, z, D T0.2 is skipped due to the first If/Else logic, then it checks the second if ErrorLevel command, sees that ErrorLevel is zero, and executes the else section Send double.
Neuromancer
Posts: 10
Joined: 24 May 2015, 05:02

Re: single press, double press and long press

01 Nov 2015, 11:49

Code: Select all


$z::
	KeyWait, z, T0.1
	
	if (ErrorLevel)
		Send long

	else {
		KeyWait, z, D T0.1

		if (ErrorLevel)
			Send single
		
		else
			Send double
	}

	KeyWait, z
return

this works great for me
User avatar
schwarzgrau
Posts: 23
Joined: 01 Nov 2015, 11:16
Location: Germany
Contact:

Re: single press, double press and long press

01 Nov 2015, 16:21

Wow, I'm a bit flattened by the fast and helpful responses.

Exaskryz : Oh of course, the brackets! I always forget simple stuff like this, if I try to learn a new programming language. Thank you.
Neuromancer : Thank you a lot, your script works flawlessly.
morsecode : I will check them out, thank you.
User avatar
schwarzgrau
Posts: 23
Joined: 01 Nov 2015, 11:16
Location: Germany
Contact:

Re: single press, double press and long press

01 Nov 2015, 17:14

Maybe you could help we with a followup question: I would like to use this function on the windows key. I tried to use $#:: since I expected the hashtag to be the windows key and {#} to be the hashtag, but then my script used the hashtag-key. However it worked with $Lwin, but only if I press the windows-key once (sending out "single"), double-tapping it and long-pressing it both seem to be a function on it's own in windows 10, which I didn't overwrote with my script.
Is it possible to deactivate the windows-key completely in one application and still use it as a hotkey?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: single press, double press and long press

01 Nov 2015, 21:16

I think what you're looking for is to use the #IfWinActive directive. One way to disable the Lwin key, which is an example in the hotkeys documentation, is to use LWin::Return.
User avatar
schwarzgrau
Posts: 23
Joined: 01 Nov 2015, 11:16
Location: Germany
Contact:

Re: single press, double press and long press

02 Nov 2015, 09:21

I found this on the documentation, but if I use Lwin::Return I completely disable it and can't use it afterwards right?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: single press, double press and long press

02 Nov 2015, 10:10

That would be the case. Since you said you wanted it to be completely disabled in one application, I thought that was the answer you were looking for. You can use it as a standalone hotkey, just format it like any other hotkey.

Code: Select all

#IfWinActive, Untitled - Notepad
LWin::MsgBox You pressed LWin while in notepad with an unsaved file

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], JerkyJesse, thegig4 and 238 guests