Triple actions for a single key, but different Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
xushaofa
Posts: 2
Joined: 08 Dec 2023, 05:15

Triple actions for a single key, but different

Post by xushaofa » 08 Dec 2023, 05:31

Hello.

I'd like to create a script with triple actions for a single key (left Alt): single tap, double tap and hold. I know there are many threads about this already, but the action I want for holding the button is a bit different and I simply don't know how to do that part, so I'd appreciate some help.

This is what I want:

Single tap: left click
Double tap: type "ABCD"
Hold: default behavior of left Alt when holding it, for example using it for Alt codes, such as Alt+64 for typing @

The code I have so far for the single tap and double tap:

Code: Select all

LAlt::
KeyWait, LAlt
KeyWait, LAlt, D T0.2
if ErrorLevel
{
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
	return
}
else
{
	Playspeed:=2
	Loop, 1
	{
		SetTitleMatchMode, 2
		CoordMode, Mouse, Screen
		Send, ABCD
		Sleep, % 400 //playspeed
	}
	return
}
return

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions for a single key, but different  Topic is solved

Post by Rohwedder » 08 Dec 2023, 07:31

Hallo,
try:

Code: Select all

~LAlt::
KeyWait, LAlt, T0.5
IF ErrorLevel
{
	; ToolTip, Hold
	KeyWait, LAlt
	Return
}
KeyWait, LAlt, D T0.2
if ErrorLevel
{
	; ToolTip, Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
}
else
{
	; ToolTip, Double
	KeyWait, LAlt
	Playspeed:=2
	SetTitleMatchMode, 2
	CoordMode, Mouse, Screen
	Send, ABCD
	Sleep, % 400//playspeed
}
return

xushaofa
Posts: 2
Joined: 08 Dec 2023, 05:15

Re: Triple actions for a single key, but different

Post by xushaofa » 08 Dec 2023, 09:43

@Rohwedder Thank you!

jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 08 Dec 2023, 20:41

@Rohwedder. Very nice. Have a quick question. If I wanted to eliminate the hold and add say 2 more taps could it be done. IE 1.Send {Enter},2. Run, Something,and a couple of more options? Tk's

Code: Select all

~LAlt::
KeyWait, LAlt, T0.5
IF ErrorLevel
{
	; ToolTip, Hold
	KeyWait, LAlt
	Return
}
KeyWait, LAlt, D T0.2
if ErrorLevel
{
	; ToolTip, Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
}
else
{
	; ToolTip, Double
	KeyWait, LAlt
	Playspeed:=2
	SetTitleMatchMode, 2
	CoordMode, Mouse, Screen
	Send, ABCD
	Sleep, % 400//playspeed
}
return

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions for a single key, but different

Post by Rohwedder » 09 Dec 2023, 03:07

I recommend Morse().
The respective Hotkey is used to morse a pattern consisting of zeros (short) and ones (long). Here is your script ( incl. hold ):

Code: Select all

~LAlt::
Switch, Morse() {
Case "0": ; Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
Case "00": ; Double
	Playspeed:=2
	Loop, 1
	{
		SetTitleMatchMode, 2
		CoordMode, Mouse, Screen
		Send, ABCD
		Sleep, % 400//playspeed
	}
Case "1": ; Hold
Default:MsgBox,, Pattern without Case,% Pattern, 3
} Return

Morse(Timeout = 400) {
    Global Pattern := ""
	RegExMatch(A_ThisHotkey, "\W$|\w*$", Key)
    While, !ErrorLevel {
        T := A_TickCount
        KeyWait %Key%
        Pattern .= A_TickCount-T > Timeout
        KeyWait %Key%,% "DT" Timeout/1000
    } Return Pattern
}
The MsgBox shows Patterns to which no Cases have yet been assigned.

jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 09 Dec 2023, 05:55

@Rohweddar. Thank you for your quick reply. Little confused as I am still a bit of a rookie. So this is what happened. I added a case as I understood it and when I reloaded it I got an error at Line 20. Default must be enclosed by switch? Not sure what that means. Before I added Case "000" everything worked fine so obviously I did something wrong. Can you help? Tk's

Code: Select all

~LAlt::
Switch, Morse() {
Case "0": ; Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
Case "00": ; Double
	Playspeed:=2
	Loop, 1
	{
		SetTitleMatchMode, 2
		CoordMode, Mouse, Screen
		Send, ABCD
		Sleep, % 400//playspeed
	}
Case "000": ;Triple
MsgBox, 4, , Do you want to continue? (Press YES or NO)
IfMsgBox No
    	
Case "1": ; Hold
Default:MsgBox,, Pattern without Case,% Pattern, 3
} Return

Morse(Timeout = 400) {
    Global Pattern := ""
	RegExMatch(A_ThisHotkey, "\W$|\w*$", Key)
    While, !ErrorLevel {
        T := A_TickCount
        KeyWait %Key%
        Pattern .= A_TickCount-T > Timeout
        KeyWait %Key%,% "DT" Timeout/1000
    } Return Pattern
}

jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 09 Dec 2023, 07:45

Got it. Something to do with the msgbox I was using in Case "000". All good now. Thank you for your help.

Code: Select all

~LAlt::
Switch, Morse() {
Case "0": ; Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
Case "00": ; Double
	Playspeed:=2
	Loop, 1
	{
		SetTitleMatchMode, 2
		CoordMode, Mouse, Screen
		Send, ABCD
		Sleep, % 400//playspeed
	}
Case "000": ;Triple
MsgBox This is the 1-parameter mode. 1234

Case "0000": ;Quadruple
MsgBox This is the 1-parameter mode. 

   	
Case "1": ; Hold
Default:MsgBox,, Pattern without Case,% Pattern, 3

} Return

Morse(Timeout = 400) {
    Global Pattern := ""
	RegExMatch(A_ThisHotkey, "\W$|\w*$", Key)
    While, !ErrorLevel {
        T := A_TickCount
        KeyWait %Key%
        Pattern .= A_TickCount-T > Timeout
        KeyWait %Key%,% "DT" Timeout/1000
    } Return Pattern
}

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions for a single key, but different

Post by Rohwedder » 09 Dec 2023, 08:22

This would have worked:

Code: Select all

Case "000": ;Triple
	MsgBox, 4, , Do you want to continue? (Press YES or NO)
	Voice := ComObjCreate("SAPI.SpVoice")
	IfMsgBox No
		Voice.Speak("Why not?")

jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 09 Dec 2023, 08:30

Thank you rohweddar. I will save your suggestion. What I have ATM does work fine unless you can see a way to neaten it up. Tk's again.

Code: Select all

~LAlt::
Switch, Morse() {
Case "0": ; Single
	DllCall("SetCursorPos", "int", 1603, "int", 942)
	MouseClick, L
Case "00": ; Double
	Playspeed:=2
	Loop, 1
	{
		SetTitleMatchMode, 2
		CoordMode, Mouse, Screen
		Send, {Enter}
		Sleep, % 400//playspeed
	}
Case "000": ;Triple
Run, "D:\PortableApps\specPortable\specPortable.exe"

Case "0000": ;Quadruple
Run, "D:\PortableApps\RevoUninstallerPortable\RevoUninstallerPortable.exe"

   	
Case "1": ; Hold
Default:MsgBox,, Pattern without Case,% Pattern, 3

} Return

Morse(Timeout = 400) {
    Global Pattern := ""
	RegExMatch(A_ThisHotkey, "\W$|\w*$", Key)
    While, !ErrorLevel {
        T := A_TickCount
        KeyWait %Key%
        Pattern .= A_TickCount-T > Timeout
        KeyWait %Key%,% "DT" Timeout/1000
    } Return Pattern
}

jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 10 Dec 2023, 21:34

@rohwedder. Last question. With regards to the Morse script which works like a charm by the way. My question is instead of having to press key all the time is there a way to modify script so all I have to do is is hold button down to get..my desired. IE 400 ms for first 0(single). 800ms for second 00(double).and so on. And I could adjust duration as needed. If it can't be done then fine. As I said I am quite happy with what you set me up with. Tk's.

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Triple actions for a single key, but different

Post by Rohwedder » 11 Dec 2023, 02:49


jrachr
Posts: 545
Joined: 01 Mar 2021, 17:33

Re: Triple actions for a single key, but different

Post by jrachr » 11 Dec 2023, 06:54

@rohwedder. Thank you. That will do it I think. Just need to set the case times so they don't react as quick. Like the soundbeep between the case's. Tk's again. Cheer's

Post Reply

Return to “Ask for Help (v1)”