@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
________________________________________________________________________________