SetTimer script triggered as long key is pressed Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

SetTimer script triggered as long key is pressed

Post by nina1000 » 30 Mar 2022, 13:09

Hi all, just registered due that got stuck with my first (nearly working) script ;)
Unfortunately when executing it keeps rotating sending the keys (the timers are considered correctly).

Target: q, w, e, r shall only be sent when keeping key ´a´ pressed, releasing ´a´will stop sending those. What did I do wrong here?
Any help would be highly appreciated.

Code: Select all

#NoEnv
#SingleInstance Force
SendMode Input
SetWorkingDir %A_ScriptDir%

SetTimer, SendLetterQ, 5000
SetTimer, SendLetterW, 500
SetTimer, SendLetterE, 2000
SetTimer, SendLetterR, 8000

a::

SendLetterQ() {
	Send, q
}

SendLetterW() {
	Send, w
}


SendLetterE() {
	Send, e
}


SendLetterR() {
	Send, r
}

return
Last edited by nina1000 on 31 Mar 2022, 00:26, edited 1 time in total.

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 30 Mar 2022, 19:27

Welcome to this AutoHotkey forum!

To disable the timer, use the "Off" value for the PeriodOnOffDelete parameter. A function does not execute when you define it; you must call it.

Code: Select all

a::
SetTimer, Q, 5000
SetTimer, W, 500
SetTimer, E, 2000
SetTimer, R, 8000
SoundBeep, 1500
KeyWait, %A_ThisHotkey%
Loop, Parse, % "QWER"
 SetTimer, %A_LoopField%, Off
SoundBeep, 1000
Return

Q:
W:
E:
R:
Send % Format("{:L}", A_ThisLabel)
Return

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 31 Mar 2022, 00:01

Just perfect, thank you so much mike :bravo:
I guess I understood the logic so far, do you want me also to help in how to add some different sleep and key press durations to it properly?
(by the way - this script is for an interactive visual and accustic tool for impaired kids)

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 31 Mar 2022, 05:09

Thank you for the feedback.

The script is yours. You are welcome to post what you like or what may be helpful to others.

More about functions if needed: Functions

Enjoy!

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 31 Mar 2022, 09:45

Thanks too - will do

Unfortunately I was not able to manage or figure out

- where/how to add in that script eg. for key ´e´send a specific sleep time of 3000ms, for key ´q´send a sleep time of 9000ms
(pressing down time)

Hope you don´t mind assisting again on that additonal code?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 31 Mar 2022, 18:50

You can change the subroutine to send up and down actions separately, with Sleep between them. See Send. If needed, you can have a separate subroutine for each letter.

Considering your new requirement, I'm not sure that your timer frequencies still make sense. You can adjust them if needed.

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 01 Apr 2022, 01:21

Hi mike, thank you, I did as suggested.
I just figured out, the application sometimes not accept key send requests - please do not ask me why.

As a workaround I added like this, its working.
(hope from coding perspective this is not a mess)

Code: Select all

Q:
sleep 250
W:
sleep 250
E:
sleep 250
R:
sleep 500

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 01 Apr 2022, 04:34

I do not see how that changes your press duration, but great if it works!

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 01 Apr 2022, 04:37

honestly it does not, i used the original one :roll:

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 01 Apr 2022, 04:45

I'll be offline for a while, but an example is below.

Code: Select all

a::
SetTimer, Q, 3000
SoundBeep, 1500
Gosub, Q
KeyWait, a
SetTimer, Q, Off
SoundBeep, 1000
Return

Q:
Send {q down}
Sleep, 1000
Send {q up}
SoundBeep, 800
Return

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 01 Apr 2022, 08:25

Thank you mike, I just ried to understand and multiply this to q, w, e, r, keys.
What I figured out is that the timers will not be considered plus releasing ´a´key will also not stop it anymore.
I am a bit confused right now..

Code: Select all

#NoEnv
#Warn
#SingleInstance Force
SendMode Input
SetWorkingDir %A_ScriptDir%

a::
SetTimer, Q, 5000
; SoundBeep, 1500
Gosub, Q
KeyWait, a
SetTimer, Q, Off
; SoundBeep, 1000

SetTimer, W, 3000
; SoundBeep, 1500
Gosub, W
KeyWait, a
SetTimer, W, Off
; SoundBeep, 1000

SetTimer, E, 2000
; SoundBeep, 1500
Gosub, E
KeyWait, a
SetTimer, E, Off
; SoundBeep, 1000

SetTimer, R, 8000
; SoundBeep, 1500
Gosub, R
KeyWait, a
SetTimer, R, Off
; SoundBeep, 1000

Return

Q:
Send {q down}
Sleep, 1000
Send {q up}
; SoundBeep, 800

W:
Send {w down}
Sleep, 1500
Send {w up}
; SoundBeep, 800

E:
Send {e down}
Sleep, 1000
Send {e up}
; SoundBeep, 800

R:
Send {r down}
Sleep, 2000
Send {r up}
; SoundBeep, 800

Return

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 01 Apr 2022, 13:08

Your idea is OK, but you have too many KeyWaits, and too few Returns. Some ideas are below.

Code: Select all

num := {Q: [5, 1], W: [3, 1.5], E: [2, 1], R: [8, 2]} ; [frequencySeconds, pressDurationSeconds]

a::
SoundBeep, 1500
For letter, number in num
 SetTimer, %letter%, % 1000 * number.1
Loop, Parse, % "QWER"
 Gosub, %A_LoopField%
KeyWait, a
For letter in num
 SetTimer, %letter%, Off
SoundBeep, 1000
Return

Q:
W:
E:
R:
Send % "{" (key := Format("{:L}", A_ThisLabel)) " down}"
Loop, % 1000 * num[A_ThisLabel].2 / 250
 Sleep, 250
Until !GetKeyState("a", "P")
Send {%key% up}
Return
Possibly better:

Code: Select all

a::
Loop, Parse, % "QWER"
 SetTimer, %A_LoopField%, -50
KeyWait, a
Loop, Parse, % "QWER"
 SetTimer, %A_LoopField%, Off
Return

Q:
Send {q down}
Sleep, 1000
Send {q up}
SetTimer,, -4000
Return

W:
Send {w down}
Sleep, 1500
Send {w up}
SetTimer,, -1500
Return

E:
Send {e down}
Sleep, 1000
Send {e up}
SetTimer,, -1000
Return

R:
Send {r down}
Sleep, 2000
Send {r up}
SetTimer,, -6000
Return
Alternatively, the Sleep could be converted into new timers. Lots of options here....

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 02 Apr 2022, 09:02

Hello Mike, thank you so much for getting back to me.
I am sorry for the late response - had to wait till my shift starts and I can right try this out with the kids.
Will report back soon :wave:
nina

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 02 Apr 2022, 10:58

Overall both are working fine and nicely - please let me provide a small feedback while using it within the app

1)
+ the delays are already doing their job from the beginning on (does not matter in practice)
- it has still a delay when releasing ´a´ till it final stops (keeps still sending keys a while)

2)
+ is much much more responsive when releasing ´a´ - it stops immediately
- it ignores timers and sleep at the start, means first session of ´q w e r´ is bein pressed immediately in a row but after that its working fine
+ it is also easier for me to understand it´s logic ;)

I would like to thank you so much again for your assistance and help - warmly appreciated!
br,
nina

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed  Topic is solved

Post by mikeyww » 02 Apr 2022, 11:14

The revision below fixes the delayed stop, but I think that there are still some timing issues due to the press durations.

Code: Select all

num := {Q: [5, 1], W: [3, 1.5], E: [2, 1], R: [8, 2]} ; [frequencySeconds, pressDurationSeconds]

a::
SoundBeep, 1500
For letter, number in num
 SetTimer, %letter%, % 1000 * number.1
Loop, Parse, % "QWER"
 Gosub, %A_LoopField%
KeyWait, a
For letter in num
 SetTimer, %letter%, Off
SoundBeep, 1000
Return

Q:
W:
E:
R:
If !GetKeyState("a", "P")
 Return
Send % "{" (key := Format("{:L}", label := A_ThisLabel)) " down}"
Loop, % 1000 * num[label].2 / 250
 Sleep, 250
Until !GetKeyState("a", "P")
Send {%key% up}
Return
A basic one, where a subroutine executes after the corresponding timer expires:

Code: Select all

a::
SoundBeep, 1500
SetTimer, Q, 5000
SetTimer, W, 3000
SetTimer, E, 2000
SetTimer, R, 8000
KeyWait, a
Loop, Parse, % "QWER"
 SetTimer, %A_LoopField%, Off
SoundBeep, 1000
Return

Q:
Send {q down}
wait(1000)
Send {q up}
Return

W:
Send {w down}
wait(1500)
Send {w up}
Return

E:
Send {e down}
wait(1000)
Send {e up}
Return

R:
Send {r down}
wait(2000)
Send {r up}
Return

wait(duration, key := "a") {
 Loop, % duration / 250
  Sleep, 250 * GetKeyState(key, "P")
}

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 02 Apr 2022, 11:22

thanks, will give this a try after preparing dinner
(you challenged me with the code continuing on the first one, right? need some extra time to study and understand :angel: )

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: SetTimer script triggered as long key is pressed

Post by mikeyww » 02 Apr 2022, 12:13

Could be, but I'd say that dinner is more important. :)

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: SetTimer script triggered as long key is pressed

Post by nina1000 » 02 Apr 2022, 12:16

From use experience no difference at all, guess I will still focus in learning on the second one first :)
No issues found in both at all, enjoying it!
:thumbup: Thanks Mike! :thumbup:
br,
nina

Post Reply

Return to “Ask for Help (v1)”