Long press is not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Long press is not working

Post by Joey5 » 05 Apr 2024, 06:17

I hope I can get help for the following. I found several flavors of these but I could not make any work on long press. I want the script to send d with short press and e with long press. Is it possible. The attached script send d with short press but send ed with long press. It send e and then also d when I release the key. I tried to add d up before the iferror line but that keeps sending dddd...
Thank you.

Code: Select all

$d::
keywait, d, T0.5 

if errorlevel
 
Send e

else

Send d

return
[Mod edit: Fixed [code][/code] tags. They belong around the code.]

User avatar
V0RT3X
Posts: 366
Joined: 20 May 2023, 21:59
Contact:

Re: Long press is not working

Post by V0RT3X » 05 Apr 2024, 06:56

Haven't tested myself, but I would have a look at Rohwedder's code at...

viewtopic.php?f=76&t=125300&p=556712&hilit=long+press#p556696

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

Re: Long press is not working  Topic is solved

Post by Rohwedder » 06 Apr 2024, 03:31

Hallo,
or:

Code: Select all

$d::
keywait, d, T0.5
if errorlevel
	Send e
else
	Send d
keywait, d
; d key down would immediately trigger $d:: again
return
or with Ternary operator:

Code: Select all

$d::
keywait, d, T0.5
Send,% ErrorLevel?"e":"d"
keywait, d
; d key down would immediately trigger $d:: again
return
or with v2:

Code: Select all

#Requires AutoHotkey v2.0
$d::Send(KeyWait("d", "T0.5")?"e":"d"), KeyWait("d")

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

Re: Long press is not working

Post by jrachr » 06 Apr 2024, 08:15

Good day. So if I understand this by pressing d once it will delay .5 seconds and run this ahk? Or work as a normal d key(IE press dd in notepad,etc). It does not.I also tried holding down for a sec before releasing. This would be ideal for me if I can get to work.So question is can it be made to run ahk? Thank you.

Code: Select all

$d::
keywait, d, T0.5
if errorlevel
	Run, C:\Users\Jr\Desktop\Mute2.ahk
else
	Send d
keywait, d
; d key down would immediately trigger $d:: again
return

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 06 Apr 2024, 08:49

@Rohwedder thank you so much. This will be extremely helpful to me. Greatly appreciate it. Hoping for two modification if possible.

1) beeps when I have pressed it for 0.5 sec.

2) Is it possible to insert winactivate? e.g. I want long press to be only applicable to a specific program, say notepad. But want short press to be applicable universally.

Thank you again.

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 06 Apr 2024, 08:52

V0RT3X wrote:
05 Apr 2024, 06:56
Haven't tested myself, but I would have a look at Rohwedder's code at...

viewtopic.php?f=76&t=125300&p=556712&hilit=long+press#p556696
Thank you.

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 06 Apr 2024, 09:01

I tried to add soundbeep as below but it gives error.

Code: Select all

$d::
keywait, d, T0.5
if errorlevel

Soundbeep
	Send e

else
	Send d
keywait, d
; d key down would immediately trigger $d:: again
return
[Mod edit: Fixed [code][/code] tags. They should go around the code.]

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

Re: Long press is not working

Post by Rohwedder » 06 Apr 2024, 09:13

Code: Select all

#IFWinActive, ahk_exe notepad.exe
$d::
keywait, d, T0.5
if errorlevel {
	SoundBeep
	Send e
} else Send d
keywait, d
; d key down would immediately trigger $d:: again
return
#IFWinActive

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

Re: Long press is not working

Post by jrachr » 06 Apr 2024, 09:20

@rohwedder. My bad again.Should have made clear who my question was directed at. Anyways. Is this doable?

Code: Select all

$d::
keywait, d, T0.5
if errorlevel
	Run, C:\Users\Jr\Desktop\Mute2.ahk
else
	Send d
keywait, d
; d key down would immediately trigger $d:: again
return

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 06 Apr 2024, 09:38

@Rohwedder
I have to try it more but on first blush... brilliant. Opens up so many options for me. Thank you so much. 8-)

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 06 Apr 2024, 14:02

@Rohwedder the following script works like a charm. Thank you again.
Is it possible to incorporate double tapping in it?
Currently, it does the following when in notepad.
single press sends d
long press sends e

Is it possible for it to send "f" with double tapping of "d" key?
Thank you in advance for looking into it. I know I am being greedy but it will be helpful. I have something which I am happy to post but it sends D only alternatively.

Code: Select all

#IFWinActive, ahk_exe notepad.exe
$d::
keywait, d, T0.5
if errorlevel {
	SoundBeep
	Send e
} else Send d
keywait, d
; d key down would immediately trigger $d:: again
return
#IFWinActive
[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]
Last edited by gregster on 06 Apr 2024, 19:24, edited 1 time in total.
Reason: Fixed again the code tags. Please see also the PM I sent you.

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

Re: Long press is not working

Post by Rohwedder » 07 Apr 2024, 03:13

@jrachr Why not? If it doesn't do what it's supposed to do, what should it do instead?
There were not enough tangible requirements in your first post.

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

Re: Long press is not working

Post by Rohwedder » 07 Apr 2024, 03:47

Then try:

Code: Select all

#IFWinActive, ahk_exe notepad.exe
$d::
Switch, Morse() {
Case "0":Send d ;single short press sends d
Case "1":Send e ; single long press sends e
Case "00":Send f ; send "f" with double tapping of "d" key
Default:MsgBox,, Pattern without Case,% Pattern, 3
} Return
#IFWinActive

Morse(Timeout = 500) {
    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 respective Hotkey is used to morse a pattern consisting of “0” (short) and “1” (long).
The MsgBox shows Patterns to which no Cases have yet been assigned.
If the Hotkey consists of several keys (Modifier or & is used), the Morse code of the rightmost key counts.

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 07 Apr 2024, 07:45

@Rohwedder thank you so much for your prompt help. I really appreciate your time and help. And others on this board. Based on my browsing on this issue, a script like this will be nice to include in the scripts and functions section. Is it something I can do? Or only you have the right to do that?

The problem with similar scripts I had tried thus far partially messed up typing in other programs like word, browser etc. By adding, IfWinActive... at the start and IfWinActive at the end, that problem does not occur. Brilliant!

In case, I need it, I added a line for triple press. Also, for my program, .350 should work better so I changed that. TY.

Code: Select all

#IFWinActive, ahk_exe notepad.exe
$d::
Switch, Morse() {
Case "0":Send d ;single short press sends d
Case "1":Send e ; single long press sends e
Case "00":Send f ; send "f" with double tapping of "d" key
Case "000":Send g ; send "g" with double tapping of "d" key
Default:MsgBox,, Pattern without Case,% Pattern, 3
} Return
#IFWinActive

Morse(Timeout = 350) {
    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: Long press is not working

Post by jrachr » 07 Apr 2024, 07:52

@rohwedder. I probably have put my Run, C:\Users\Jr\Desktop\Mute2.ahk in to run the wrong place. All i wanted was to long press d to Run the ahk. but still have normal use of the d key. Any help would be appreciated.Thank You.

Code: Select all

$d::
keywait, d, T0.5
if errorlevel
	Run, C:\Users\Jr\Desktop\Mute2.ahk
else
	Send d
keywait, d
; d key down would immediately trigger $d:: again
return

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 07 Apr 2024, 08:08

I researched and understood the code (I think). But I do not understand the following line and what happens if I change the time in the following line. TY.

Code: Select all

 KeyWait %Key%,% "DT" Timeout/1000
Full code.

Code: Select all

; "d" key sends all coeds

#IFWinActive, ahk_exe notepad.exe
$d::
Switch, Morse() {

Case "0":Send d ;single short press sends d

Case "00":Send f ; send "f" with double tapping of "d" key

Case "000":Send g ; send "g" with double tapping of "d" key

Case "1":Send e ; single long press sends e

Default:MsgBox,, Pattern without Case,% Pattern, 3

} Return
#IFWinActive

Morse(Timeout = 350) {
    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: 7904
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Long press is not working

Post by Rohwedder » 07 Apr 2024, 09:19

Laszlo's original has been in the scripts and functions section since time immemorial!
https://www.autohotkey.com/board/topic/15574-morse-find-hotkey-press-and-hold-patterns/
but it failed with Hotkeys like ~LButton & w:: (Select text with the Lbutton down and then morse with w)
I use ~LButton & ... :: if I want to do anything with highlighted text.
Copy, manipulate, look up in Wikipedia, search for files in Everything ...

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 07 Apr 2024, 09:25

Thank you.

Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Long press is not working

Post by Joey5 » 08 Apr 2024, 08:15

@jrachr I am not an expert but I think you file has to be in the same directory where ahk program is installed. Please make sure of that. Otherwise, you have to add a path to the program. I think.

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

Re: Long press is not working

Post by jrachr » 08 Apr 2024, 10:25

@Joey5. Tk's Joey but that wasn't it.Don't worry about it. Not that important. I will figure it out. Tk's again.

Post Reply

Return to “Ask for Help (v1)”