Prevent hotkey from repeating

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Prevent hotkey from repeating

15 Sep 2018, 16:32

Here is very simple function to prevent hotkey repeating:

(I mean, when we press some hotkey, but continue to keep our fingers on buttons, instead of quickly release them).

Code: Select all

PreventRepeating() {
    KeyWait, Alt
    KeyWait, Ctrl
    KeyWait, Shift
    KeyWait, LWin
    KeyWait, RWin
    KeyWait, %A_ThisHotkey%
}

F1::
    SendInput, Foo
    PreventRepeating()
Return

^a::
    SendInput, Bar
    PreventRepeating()
Return
It works for me and seems to be very useful, but why I haven't seen something similar before? Probably it have some pitfalls?

Also, I could reformulate my question: what is the good way to prevent hotkey from repeating?
Last edited by john_c on 15 Sep 2018, 16:42, edited 1 time in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Prevent hotkey from repeating

15 Sep 2018, 16:39

You could play with the UP versions of the respective hotkeys, DRock did something similar and it works well for his case.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

15 Sep 2018, 17:13

wolf_II wrote:You could play with the UP versions of the respective hotkeys, DRock did something similar and it works well for his case.
Thanks! I tried to find DRock on forum and with Google, but haven't found anything. Probably you have a link?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Prevent hotkey from repeating

15 Sep 2018, 17:23

Sorry, I'm afraid the topic starts off entirely unrelated. on page 3, he posts a script where he uses a lot of UP versions and claims (If I understand correctly) that somehow keyrepeat rates are swallowed. I may have used the wrong words here, but that's how I understand him. First look: seems to work.
https://autohotkey.com/boards/viewtopic ... 74#p238674
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

15 Sep 2018, 17:39

@wolf_II Thanks, I will look at it!
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Prevent hotkey from repeating

15 Sep 2018, 20:52

Yes hes right. But ive seen the other topic with you amd lexikos on KeyWait and I was also asking whats the best way to acheive this.

But in the end the only down side if you consider it as so... is that the key dont send its output until released so it has a delay of the press duration...

What about keywait? Is it instantly responding and also not repeating? If so maybe its better for speed than %key% UP::
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Prevent hotkey from repeating

15 Sep 2018, 21:56

DRocks wrote:What about keywait? Is it instantly responding and also not repeating? If so maybe its better for speed than %key% UP::
The biggest problem with keywait you'll encounter is due to threading, when you're implementing it on multiple keys and press and hold them. The keywait of the first key will be interrupted by the second key until the second key is released, regardless whether the first key has been released (and possibly pressed again) already.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Prevent hotkey from repeating

15 Sep 2018, 22:07

Ok thanks! So in the case you never use hotkeys together, KeyWait may be more efficient than Key UP:: for instant reaction?

For example if I use
Key UP::
GuiControlGet, bla, blablabla
If (inStr( bla, ".")
Return
Else send "."
Return

Would this be analyzing slower than KeyWait?
Considering I also want to prevent key repeats that would request useless evaluations repeatedly.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Prevent hotkey from repeating

15 Sep 2018, 22:28

KeyWait polls for a key state, and the polling will be paused when AHK is processing a different hotkey. Up hotkeys trigger on an event, which will usually interrupt anything already running (still, plenty of exceptions).

If you're not pressing multiple keys at the same time, you'll probably not notice any significant timing difference. KeyWait is usually easier to implement.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

16 Sep 2018, 06:40

Nextron wrote:The biggest problem with keywait you'll encounter is due to threading, when you're implementing it on multiple keys and press and hold them. The keywait of the first key will be interrupted by the second key until the second key is released, regardless whether the first key has been released (and possibly pressed again) already.
Hi! Could you post such example please? :)
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Prevent hotkey from repeating

16 Sep 2018, 06:54

Code: Select all

/*
• Press and hold F1
• Press and hold F2
• Release F1: Nothing happens because F1's thread is interrupted by F2's thread.
• Release F2: Msgbox F2 displays as expected. Then F1's thread is resumed and F1's msgbox is displayed.
*/
F1::
	KeyWait,F1
	MsgBox F1 released
Return
F2::
	KeyWait,F2
	MsgBox F2 released
Return
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Prevent hotkey from repeating

16 Sep 2018, 07:06

Hmmm ok thank you again it clarifies some aspects theorically.

So if we keep the example stated above

Code: Select all

 For example if I use 
Key UP::
GuiControlGet, bla, blablabla
If (inStr( bla, ".") 
Return 
Else send "."
Return
Would it be logical and efficient to do something like

Code: Select all

NumpadDot Down::
GuiControlGet, outputvar, ....., .....
If (inStr(outputvar, ".")
Return
Else
Send .
Keywait,......, UP
Explanation of the goal here: NumpadDot down evaluates instantly if a dot is in the current controls string, so you never waste a fraction of a second on this part. If not send dot. If yes return

Then lets say to be 100% sure there wont be repeated calls we use Keywait before the final return?

What do you think about that? I know I will test it but theorically would it be the most logical way to intercept repeats, detect presence in str and max speed?
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Prevent hotkey from repeating

16 Sep 2018, 07:18

Hallo,
try:

Code: Select all

F1::
	PreventRepeating()
	SendInput, Foo
Return
^a::
    PreventRepeating()
	SendInput, Bar
Return

PreventRepeating()
{
	Static	PrevRep := Object()
	If PrevRep[A_ThisHotkey]
		Exit
	IF InStr(A_ThisHotkey,"Up")
	{
		PrevRep[SubStr(A_ThisHotkey,1,-3)] := False
		Exit
	}	
	PrevRep[A_ThisHotkey] := True
	Hotkey, %A_ThisHotkey%%A_Space%Up,%A_ThisHotkey%
}
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

16 Sep 2018, 07:21

Nextron wrote:...
@Nextron Thanks, it was very useful! Now I understand.

So, it seems that for simple cases, when we need to launch some action immediately, placing KeyWait after this action (instead of before), is the best solution?

Code: Select all

F1::
	MsgBox F1 released
    KeyWait,F1
Return
F2::
	MsgBox F2 released
	KeyWait,F2
Return
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

16 Sep 2018, 07:38

Rohwedder wrote:...
Hallo :)

Looks very interesting. I will need some time today to figure out how it works! :)

I understand, your script intended to avoid some possible pitfalls of my original version and you have much more knowledge about AutoHotkey.

But could you please elaborate at which exactly point it is better then mine?

And again, thanks for your version :)

Update:

Well, I see, it really works much better! Here is an example: https://autohotkey.com/boards/viewtopic ... 79#p238879
Last edited by john_c on 16 Sep 2018, 08:06, edited 1 time in total.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Prevent hotkey from repeating

16 Sep 2018, 07:54

john_c wrote:So, it seems that for simple cases, when we need to launch some action immediately, placing KeyWait after this action (instead of before), is the best solution?
Not really. Try the example, by holding F1, holding F2, and then press F1 a couple of times and holding it again. Then release F2. The pressing of F1 didn't trigger anything and were functionally invisible to the script.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Prevent hotkey from repeating

16 Sep 2018, 08:04

Nextron wrote:...
@Nextron Hm, yes, I see.

I press F1, then F2. Then release F1 and try to press it again - nothing happens.

Update: Just discovered, that Rohwedder's example works correctly for such case! :)
User avatar
SteveMylo
Posts: 233
Joined: 22 Jun 2021, 00:50
Location: Australia
Contact:

Re: Prevent hotkey from repeating

10 Aug 2021, 08:40

Rohwedder wrote:
16 Sep 2018, 07:18
Hallo,
try:

Code: Select all

F1::
	PreventRepeating()
	SendInput, Foo
Return
^a::
    PreventRepeating()
	SendInput, Bar
Return

PreventRepeating()
{
	Static	PrevRep := Object()
	If PrevRep[A_ThisHotkey]
		Exit
	IF InStr(A_ThisHotkey,"Up")
	{
		PrevRep[SubStr(A_ThisHotkey,1,-3)] := False
		Exit
	}	
	PrevRep[A_ThisHotkey] := True
	Hotkey, %A_ThisHotkey%%A_Space%Up,%A_ThisHotkey%
}
This worked amazing for me! thanks :superhappy:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mapcarter, Marium0505 and 377 guests