Long press is not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rohwedder
Posts: 7700
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Long press is not working

Post by Rohwedder » 18 Apr 2024, 12:10

I had to try to split the following line into 2 separate scripts, one for ^+V and L.
Separate scripts? i.e. separate .ahk files?
I do not understand!
Splitting the SendTimer Output into even more lines for the purpose of commenting works like this:

Code: Select all

keywait, d, T0.5
if errorlevel { ; long press starts SendTimer no. 1:
	SoundBeep ; Sleep 100, ^m, Sleep 1500, ^+v, Sleep 500, L
	keywait, d ; after the D key has been released
	SendTimer(1, "
	( LTrim Join C
		{100} ; Sleep 100 ms
		{Ctrl Down}{m}{Ctrl Up} ; ^m
		{1500} ; Sleep 1500 ms
		{Ctrl Down}{Shift Down}{v}{Shift Up}{Ctrl Up} ; ^+v
		{50} ; Sleep 50 ms
		{l Down}{50}{l Up} ; l hold 50 ms
	)")
} else Send d ; short press
keywait, d

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

Re: Long press is not working

Post by Joey5 » 18 Apr 2024, 19:09

@Rohwedder This is great. Thank you so much. I have created three scripts thus far from your scripts at work. They work great. Thank you. I tried to insert winactivate ahk.... program.exe so that the script is entirely app specific. However, I could not make it work. I'll try some more. Will let you know either way. Thanks.

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

Re: Long press is not working

Post by Joey5 » 19 Apr 2024, 05:32

@Rohwedder I was not being clear I think. So the first line "#IFWinActive, ahk_exe notepad.exe" does exactly what I want, that is not to execute the command on any other program than the one specified. However, in addition to this condition/ protection, is it possible to also activate the program. For example, if the "Ifwinactive" is for notepad.exe but if I have last typed in the word.exe, if I hover my mouse over always-open notepad window and type, it should type in the notepad and not word, even without manually activating notepad window. I have tried to place them in the fixed spot in the taskbar and used commands like ^#n. I have also tried click x,y or click, 1 etc but those have not always helped. May be if the script itself activates the program it is specifically intended for... Thank you for listening.

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

Re: Long press is not working

Post by Joey5 » 19 Apr 2024, 05:59

Duh. I am trying to write a circular error script. Can’t have Ifwinactive and winactive together!

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

Re: Long press is not working

Post by Rohwedder » 19 Apr 2024, 07:33

See https://www.autohotkey.com/docs/v1/lib/_IfWinActive.htm
This directive is called "#IfWinActive,..." not "#If MouseOver(Win)"!
#IfWinActive is only interested in which window is active and not which window the cursor is currently over.

Code: Select all

#IF MouseOver("ahk_class Notepad")
LButton::SoundBeep ; beeps when a Notepad window is left-clicked

MouseOver(Win) {
	MouseGetPos,,, Over
	Return, Over = WinExist(Win)
}
Both directives only check their condition when starting/not starting a hotkey thread, not while the thread is being processed.
A running SendTimer always sends its Output to the currently active window.
If another window becomes active during this period, it becomes the new destination for the rest of its Output.

Remedy:

Code: Select all

*w::
    Win := "ahk_id " WinActive("A") ; start window
	SendTimer(2, "Hello {200-1000}World!{Enter}", 100)
    ; Timer 2: Delay: 100ms, periodic, with a pause of 200-1000 ms
    WinWaitNotActive,% Win ; As soon as the start window is no longer active,
	SendTimer(-2) ; the SendTimer is deactivated
Return
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SendTimer(N, Output:="", Delay:=-100, AddRandom:=0)
{   Local D, C ;global Pseudo-Arrays: Ο%%, Τ%%, Δ%%, Ρ%%
    If (N>0 And Ο%N%>"") ;Update lock
        Return ;with Ο:Omikron, Τ:Tau, Δ:Delta, P:Rho
    Critical,% (C:=A_IsCritical)?:"Off"
    N:=Abs(N), Ο%N%:=Τ%N%:=Output, D:=Abs(Δ%N%:=Delay)
    Random, D, D, D+Ρ%N%:=AddRandom, N:=Func("T").Bind(N)
	SetTimer,% N,% (""<Output)?D*(Delay>0):"Off"
    Critical,% C?:"Off" ;restore original criticality
    Return
}
T(N) ; called by SendTimer(), Timer function
{   Static Needle:="\{([0-9\-]{2,}|%.+%)\}|\{[^\}]*\}|."
    , BlindText:="{Blind}{Text}", Label:="SendTimerOff-"
    Critical
    Random, D, D:=Abs(Δ%N%),D+Ρ%N%,RegExMatch(Τ%N%,Needle,K)
    Random, K1, !(R:=StrSplit(K1,"-")) R.1, Max(0 R.1, 0 R.2)
    SendInput,% (StrLen(K)=1)?BlindText K:(K1?:D(K))
    SetTimer,,% D:=("0"<K1 Τ%N%:=StrReplace(Τ%N%, K,,, 1))
    ?Max(D, K1?D(K1):0):(Δ%N%>0)?(D, Τ%N%:=Ο%N%):"Off"
    IF !++D And (!Ο%N%:="") And IsLabel(D:=Label N)
        SetTimer,% D, -10 ;jump to Label SendTimerOff-N
    Exit
}
D(K) ; called by T(), Deref function expands variable references
{   Static Needle := "%(.*?)%", Pos:=1
    While, Pos:=RegExMatch(K, Needle, M, Old:=Pos+StrLen(M))
        Out .= SubStr(K, Old, Pos-Old) %M1%
    Return, Out SubStr(K, Old), Pos:=1
}
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

Re: Long press is not working

Post by Joey5 » 19 Apr 2024, 09:44

Thank you. More to learn. I tried the script and it kept typing hello world over and over and again until I exited the script. Was I to incorporate this in the prior scripts in the thread. I am trying to read the link you sent.

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

Re: Long press is not working

Post by Rohwedder » 19 Apr 2024, 09:50

Did you not see my comments?
; As soon as the start window is no longer active,
; the SendTimer is deactivated

To end the SendTimer, all you had to do was activate another window.

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

Re: Long press is not working

Post by Joey5 » 19 Apr 2024, 10:18

I saw it... just did/do not understand. Need to read up about it or try to comprehend it. TY.

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

Re: Long press is not working

Post by Joey5 » 20 Apr 2024, 09:37

@Rohwedder Hello. I researched the scripts and understood as detailed below. However, I am stuck with the last part.

So here is my dilemma. How do I put together the last step so I get something like the following?

If winexist program A, then run the long press script.
If winnotexist program A, then activate program A and run long press script. Thank you.

Thank you for your time, help and patience.

We started with the following.

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
$d:: instead of d::, since the script uses the Send command to send the keys that comprise the hotkey itself

The keywait, d, T0.5 command tells the script to wait for the "d" key to be released, with a timeout of 0.5 seconds

The if errorlevel statement checks if there was an error during the keywait command, which would happen if the "d" key was released within the 0.5-second timeout.
If there was an error (i.e., the "d" key was released quickly), the script sends the "e" key using the Send e command.
If there was no error (i.e., the "d" key was held down for at least 0.5 seconds), the script sends the "d" key using the Send d command.

The final keywait, d command ensures that the script waits for the "d" key to be released before exiting the hotkey. This is necessary to prevent the hotkey from triggering itself again immediately, as the script would detect the "d" key being held down.
________________________________________________________________________________

We then added IfWinactivate and soundbeep...

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
#IFWinActive, ahk_exe notepad.exe
This line sets a context for the script to only be active when the window with the process name "notepad.exe" is the active window.

#IFWinActive
This line closes the context set at the beginning of the script, so that the script is no longer limited to only being active when the "notepad.exe" window is active.
___________________________________________________________________________________

the new addition as follows...

Code: Select all

#IFWinActive, ahk_exe notepad.exe
$*d:: ; $* both modifier required!
keywait, d, T0.5
if errorlevel {
	SoundBeep
	SendTimer(1, " ; long press starts SendTimer no. 1
	( LTrim Join C
		{Ctrl Down}{Shift Down}{h}{Shift Up}{Ctrl Up} ; ^+h
		{100} ; Sleep 100
		{Ctrl Down}{m}{Ctrl Up} ; ^m
		{500} ; Sleep 500
		L ; L
	)")
} else Send d ; short press
keywait, d
SendTimer(-1) ; SendTimer no. 1 is cancelled when key D is released
return
#IFWinActive

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SendTimer(N, Output:="", Delay:=-100, AddRandom:=0)
{   Local D, C ;global Pseudo-Arrays: Ο%%, Τ%%, Δ%%, Ρ%%
    If (N>0 And Ο%N%>"") ;Update lock
        Return ;with Ο:Omikron, Τ:Tau, Δ:Delta, P:Rho
    Critical,% (C:=A_IsCritical)?:"Off"
    N:=Abs(N), Ο%N%:=Τ%N%:=Output, D:=Abs(Δ%N%:=Delay)
    Random, D, D, D+Ρ%N%:=AddRandom, N:=Func("T").Bind(N)
	SetTimer,% N,% (""<Output)?D*(Delay>0):"Off"
    Critical,% C?:"Off" ;restore original criticality
    Return
}
T(N) ; called by SendTimer(), Timer function
{   Static Needle:="\{([0-9\-]{2,}|%.+%)\}|\{[^\}]*\}|."
    , BlindText:="{Blind}{Text}", Label:="SendTimerOff-"
    Critical
    Random, D, D:=Abs(Δ%N%),D+Ρ%N%,RegExMatch(Τ%N%,Needle,K)
    Random, K1, !(R:=StrSplit(K1,"-")) R.1, Max(0 R.1, 0 R.2)
    SendInput,% (StrLen(K)=1)?BlindText K:(K1?:D(K))
    SetTimer,,% D:=("0"<K1 Τ%N%:=StrReplace(Τ%N%, K,,, 1))
    ?Max(D, K1?D(K1):0):(Δ%N%>0)?(D, Τ%N%:=Ο%N%):"Off"
    IF !++D And (!Ο%N%:="") And IsLabel(D:=Label N)
        SetTimer,% D, -10 ;jump to Label SendTimerOff-N
    Exit
}
D(K) ; called by T(), Deref function expands variable references
{   Static Needle := "%(.*?)%", Pos:=1
    While, Pos:=RegExMatch(K, Needle, M, Old:=Pos+StrLen(M))
        Out .= SubStr(K, Old, Pos-Old) %M1%
    Return, Out SubStr(K, Old), Pos:=1
}
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SendTimer(1, " ; long press starts SendTimer no. 1
This line starts a SendTimer with the ID of 1. The SendTimer is used to execute a series of keyboard actions. It replaced a simple line of Send e.

SendTimer(-1) ; SendTimer no. 1 is cancelled when key D is released.
______________________________________________________________________________

We then got the following.

Code: Select all

keywait, d, T0.5
if errorlevel { ; long press starts SendTimer no. 1:
	SoundBeep ; Sleep 100, ^m, Sleep 1500, ^+v, Sleep 500, L
	keywait, d ; after the D key has been released
	SendTimer(1, "
	( LTrim Join C
		{100} ; Sleep 100 ms
		{Ctrl Down}{m}{Ctrl Up} ; ^m
		{1500} ; Sleep 1500 ms
		{Ctrl Down}{Shift Down}{v}{Shift Up}{Ctrl Up} ; ^+v
		{50} ; Sleep 50 ms
		{l Down}{50}{l Up} ; l hold 50 ms
	)")
} else Send d ; short press
keywait, d
( LTrim Join C
In the context of the entire script, this line is setting up a multi-line string that will be passed to the SendTimer function. The LTrim ensures that there are no leading whitespace characters, and the Join combines multiple strings (or variables) into a single string.
The purpose of this is to make the code more readable and maintainable, as it allows the actions to be defined in a more structured and organized way, rather than having a single long string of commands.
_______________________________________________________________________________

I understand the purpose of each line and command up to this point. It took some patience and necessary reading to understand. But I do now. However, I am still stumped about the following. After researching, I understand the code itself but don't understand how to incorporate that to above code to create winexist winactivate like situation, say between notepad and word for example.

________________________________________________________________________________
__________________________________________________________________________________

So my understanding of the following code...

Code: Select all

*w::
    Win := "ahk_id " WinActive("A") ; start window
	SendTimer(2, "Hello {200-1000}World!{Enter}", 100)
    ; Timer 2: Delay: 100ms, periodic, with a pause of 200-1000 ms
    WinWaitNotActive,% Win ; As soon as the start window is no longer active,
	SendTimer(-2) ; the SendTimer is deactivated
Return
The line Win := "ahk_id " WinActive("A") stores the ID of the currently active window in the variable Win.

Sending Timed Keystrokes: The SendTimer(2, "Hello {200-1000}World!{Enter}", 100) line sets up a timer-based keyboard input.

The line WinWaitNotActive,% Win waits for the active window to change from the one that was active when the script started.

SendTimer(-2) deactivates the timer with the ID 2

________________________________________________________________________________

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

Re: Long press is not working

Post by Rohwedder » 20 Apr 2024, 10:47

Everything right, except:
The keywait, d, T0.5 command tells the script to wait for the "d" key to be released, with a timeout of 0.5 seconds
The if errorlevel statement checks if there was an error during the keywait command, which would happen if the "d" key was not released within the 0.5-second timeout.
The following two lines must be adapted accordingly

About your dilemma:
If winexist program A, then run the long press script. No problem!
If winnotexist program A, then activate program A and run long press script.
Not possible! A non-existent window does not exist! Therefore it cannot be activated!
Try to open a window in your home in a place where there is no window.

I think you are confusing #IfWinExist with #IfWinActive?
An existing but inactive window can be activated with https://www.autohotkey.com/docs/v1/lib/WinActivate.htm
A non-existent window must first become existent, e.g. by starting the associated executable file (.exe, .com, .bat, etc.)
https://www.autohotkey.com/docs/v1/lib/Run.htm

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

Re: Long press is not working

Post by Joey5 » 20 Apr 2024, 11:17

@Rohwedder Thank you. I understand. If I am using long press only for one specific program, is it not possible to have a code where long press first activates that program followed by other commands?
TY.

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

Re: Long press is not working

Post by Rohwedder » 20 Apr 2024, 12:34

Try:

Code: Select all

#IFWinExist, ahk_exe notepad.exe
$*d::
keywait, d, T0.5
if errorlevel { ; = long press
	SoundBeep
	WinActivate ; activate ahk_exe notepad.exe
	WinWaitActive
	Send Hello ; or SendTimer(...
} else Send d ; short press
keywait, d
Return
After a short press of d, a "d" is sent to the currently active window
(Regardless of whether it is a Notepad window or not).
After a long press of d, the Notepad window will be activated first and then "Hello" is sent to it.

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

Re: Long press is not working

Post by Joey5 » 20 Apr 2024, 12:57

@Rohwedder Thank you again. Why am I getting an error message when I replace "Send Hello" part of the code with the following code? Thank you.

Code: Select all

SendTimer(1, "
	( LTrim Join C
		{100} ; Sleep 100 ms
		{Ctrl Down}{m}{Ctrl Up} ; ^m
		{1500} ; Sleep 1500 ms
		{Ctrl Down}{Shift Down}{v}{Shift Up}{Ctrl Up} ; ^+v
		{50} ; Sleep 50 ms
		{l Down}{50}{l Up} ; l hold 50 ms
	)")

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

Re: Long press is not working

Post by Joey5 » 20 Apr 2024, 13:06

Also, when your script messes up cap D. Until I hit enter, Shift+d types as d and D. It only converts to D after I hit enter. I notices that on Word document. While typing here, I could not type D. It only typed d when the script is open. Thanks.

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

Re: Long press is not working

Post by Rohwedder » 20 Apr 2024, 13:30

Then try:

Code: Select all

#IFWinExist, ahk_exe notepad.exe
$*d::
keywait, d, T0.5
if errorlevel { ; long press
	SoundBeep
	WinActivate ; activates ahk_exe notepad.exe
	WinWaitActive
	SendTimer(1, "
	( LTrim Join C
		{100} ; Sleep 100 ms
		{Ctrl Down}{m}{Ctrl Up} ; ^m
		{1500} ; Sleep 1500 ms
		{Ctrl Down}{Shift Down}{v}{Shift Up}{Ctrl Up} ; ^+v
		{50} ; Sleep 50 ms
		{l Down}{50}{l Up} ; l hold 50 ms
	)")
} else Send {Blind}d ; short press
keywait, d
Return

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SendTimer(N, Output:="", Delay:=-100, AddRandom:=0)
{   Local D, C ;global Pseudo-Arrays: Ο%%, Τ%%, Δ%%, Ρ%%
    If (N>0 And Ο%N%>"") ;Update lock
        Return ;with Ο:Omikron, Τ:Tau, Δ:Delta, P:Rho
    Critical,% (C:=A_IsCritical)?:"Off"
    N:=Abs(N), Ο%N%:=Τ%N%:=Output, D:=Abs(Δ%N%:=Delay)
    Random, D, D, D+Ρ%N%:=AddRandom, N:=Func("T").Bind(N)
	SetTimer,% N,% (""<Output)?D*(Delay>0):"Off"
    Critical,% C?:"Off" ;restore original criticality
    Return
}
T(N) ; called by SendTimer(), Timer function
{   Static Needle:="\{([0-9\-]{2,}|%.+%)\}|\{[^\}]*\}|."
    , BlindText:="{Blind}{Text}", Label:="SendTimerOff-"
    Critical
    Random, D, D:=Abs(Δ%N%),D+Ρ%N%,RegExMatch(Τ%N%,Needle,K)
    Random, K1, !(R:=StrSplit(K1,"-")) R.1, Max(0 R.1, 0 R.2)
    SendInput,% (StrLen(K)=1)?BlindText K:(K1?:D(K))
    SetTimer,,% D:=("0"<K1 Τ%N%:=StrReplace(Τ%N%, K,,, 1))
    ?Max(D, K1?D(K1):0):(Δ%N%>0)?(D, Τ%N%:=Ο%N%):"Off"
    IF !++D And (!Ο%N%:="") And IsLabel(D:=Label N)
        SetTimer,% D, -10 ;jump to Label SendTimerOff-N
    Exit
}
D(K) ; called by T(), Deref function expands variable references
{   Static Needle := "%(.*?)%", Pos:=1
    While, Pos:=RegExMatch(K, Needle, M, Old:=Pos+StrLen(M))
        Out .= SubStr(K, Old, Pos-Old) %M1%
    Return, Out SubStr(K, Old), Pos:=1
}
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

Re: Long press is not working

Post by Joey5 » 20 Apr 2024, 14:08

Thank you for all your help. It is working on notepad, word etc. will try at work. TY.

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

Re: Long press is not working

Post by Joey5 » 21 Apr 2024, 12:07

@Rohwedder Hello. I tried to add Morse() "00" to have double tapping feature. Used the following modified code. However, it sends 1 and then Win+3 instead of Win+3 without 1 when I double tap 1. Long press works fine and send Win+2. Can you please help? TY.

Code: Select all

#IFWinExist, ahk_exe notepad.exe
$*1::
keywait, 1, T0.5
if errorlevel { ; long press
    SoundBeep
    WinActivate ; activates ahk_exe notepad.exe
    WinWaitActive
    SendTimer(1, "
    ( LTrim Join C
        {100} ; Sleep 100 ms
        
        {LWin Down}{2}{LWin Up} 
        {50} ; Sleep 50 ms
        
    )")

} else {
    Morse("1")
}

keywait, 1
Return

Morse(key) {
    static lastTap := 0
    currentTap := A_TickCount
    if (currentTap - lastTap < 500) {

SendTimer(2, "
    ( LTrim Join C
        {100} ; Sleep 100 ms
        
        {LWin Down}{3}{LWin Up} 
        {50} ; Sleep 50 ms
        
    )")


    } else {
        Send, %key%
    }
    lastTap := currentTap
}

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SendTimer(N, Output:="", Delay:=-100, AddRandom:=0)
{   Local D, C ;global Pseudo-Arrays: Ο%%, Τ%%, Δ%%, Ρ%%
    If (N>0 And Ο%N%>"") ;Update lock
        Return ;with Ο:Omikron, Τ:Tau, Δ:Delta, P:Rho
    Critical,% (C:=A_IsCritical)?:"Off"
    N:=Abs(N), Ο%N%:=Τ%N%:=Output, D:=Abs(Δ%N%:=Delay)
    Random, D, D, D+Ρ%N%:=AddRandom, N:=Func("T").Bind(N)
    SetTimer,% N,% (""<Output)?D*(Delay>0):"Off"
    Critical,% C?:"Off" ;restore original criticality
    Return
}
T(N) ; called by SendTimer(), Timer function
{   Static Needle:="\{([0-9\-]{2,}|%.+%)\}|\{[^\}]*\}|."
    , BlindText:="{Blind}{Text}", Label:="SendTimerOff-"
    Critical
    Random, D, D:=Abs(Δ%N%),D+Ρ%N%,RegExMatch(Τ%N%,Needle,K)
    Random, K1, !(R:=StrSplit(K1,"-")) R.1, Max(0 R.1, 0 R.2)
    SendInput,% (StrLen(K)=1)?BlindText K:(K1?:D(K))
    SetTimer,,% D:=("0"<K1 Τ%N%:=StrReplace(Τ%N%, K,,, 1))
    ?Max(D, K1?D(K1):0):(Δ%N%>0)?(D, Τ%N%:=Ο%N%):"Off"
    IF !++D And (!Ο%N%:="") And IsLabel(D:=Label N)
        SetTimer,% D, -10 ;jump to Label SendTimerOff-N
    Exit
}
D(K) ; called by T(), Deref function expands variable references
{   Static Needle := "%(.*?)%", Pos:=1
    While, Pos:=RegExMatch(K, Needle, M, Old:=Pos+StrLen(M))
        Out .= SubStr(K, Old, Pos-Old) %M1%
    Return, Out SubStr(K, Old), Pos:=1
}
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

Re: Long press is not working

Post by Rohwedder » 22 Apr 2024, 01:59

Either $*1::
keywait, 1, T0.5
or $*1::
Switch, Morse()
!
Both methods record your tapping of key 1. If both are arranged one after the other, the second method can no longer record anything, as you have already stopped tapping.
Since Morse() is "more intelligent":

Code: Select all

#IFWinExist, ahk_exe notepad.exe
$*1::
Send, {Blind}1 ; writes Case "0" character
Switch, Morse()
{
Case "0":Return ; short press, character was already written
Case "1": ; long press
Send, {Bs} ; removes Case "0" character
SoundBeep
WinActivate ; activates ahk_exe notepad.exe
WinWaitActive
SendTimer(1, "
( LTrim Join C
	{100} ; Sleep 100 ms
	
	{LWin Down}{2}{LWin Up} 
	{50} ; Sleep 50 ms
	
)")
Case "00": ; double tapping
Send, {Bs} ; removes Case "0" character
SendTimer(2, "
    ( LTrim Join C
        {100} ; Sleep 100 ms
        
        {LWin Down}{3}{LWin Up} 
        {50} ; Sleep 50 ms
        
    )")
Default:
Send, {Bs} ; removes Case "0" character
MsgBox,, Pattern without Case,% Pattern, 3
} Return
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
}
SendTimer(N, Output:="", Delay:=-100, AddRandom:=0)
{   Local D, C ;global Pseudo-Arrays: Ο%%, Τ%%, Δ%%, Ρ%%
    If (N>0 And Ο%N%>"") ;Update lock
        Return ;with Ο:Omikron, Τ:Tau, Δ:Delta, P:Rho
    Critical,% (C:=A_IsCritical)?:"Off"
    N:=Abs(N), Ο%N%:=Τ%N%:=Output, D:=Abs(Δ%N%:=Delay)
    Random, D, D, D+Ρ%N%:=AddRandom, N:=Func("T").Bind(N)
    SetTimer,% N,% (""<Output)?D*(Delay>0):"Off"
    Critical,% C?:"Off" ;restore original criticality
    Return
}
T(N) ; called by SendTimer(), Timer function
{   Static Needle:="\{([0-9\-]{2,}|%.+%)\}|\{[^\}]*\}|."
    , BlindText:="{Blind}{Text}", Label:="SendTimerOff-"
    Critical
    Random, D, D:=Abs(Δ%N%),D+Ρ%N%,RegExMatch(Τ%N%,Needle,K)
    Random, K1, !(R:=StrSplit(K1,"-")) R.1, Max(0 R.1, 0 R.2)
    SendInput,% (StrLen(K)=1)?BlindText K:(K1?:D(K))
    SetTimer,,% D:=("0"<K1 Τ%N%:=StrReplace(Τ%N%, K,,, 1))
    ?Max(D, K1?D(K1):0):(Δ%N%>0)?(D, Τ%N%:=Ο%N%):"Off"
    IF !++D And (!Ο%N%:="") And IsLabel(D:=Label N)
        SetTimer,% D, -10 ;jump to Label SendTimerOff-N
    Exit
}
D(K) ; called by T(), Deref function expands variable references
{   Static Needle := "%(.*?)%", Pos:=1
    While, Pos:=RegExMatch(K, Needle, M, Old:=Pos+StrLen(M))
        Out .= SubStr(K, Old, Pos-Old) %M1%
    Return, Out SubStr(K, Old), Pos:=1
}
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
By the way, nobody is forcing you to use SendTimer()! A simple sequence of sends and sleeps is also sufficient here.
At some point in this long story I thought the script should do several things at the same time and therefore suggested SendTimer().

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

Re: Long press is not working

Post by Joey5 » 22 Apr 2024, 18:36

It has indeed been a long story... And only because you have patiently helped me by explaining and providing with the scripts and then some more. Today, I incorporated a lot of this at work. I am super excited about how it will help me tremendously. Did not get a chance to try the Morse script you provided last night. Hope to try tomorrow. TY.

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

Re: Long press is not working

Post by Joey5 » 23 Apr 2024, 05:53

Does "( LTrim Join C" or some other part of the code disable "\| key on the keyboard. The key stops working when the script is on. Does not start working even if I exit but works after rebooting the computer. Same thing happens if I run the code again.

Post Reply

Return to “Ask for Help (v1)”