A way to debounce hotkeys

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

A way to debounce hotkeys

18 Dec 2023, 15:40

I have hotkeys like:

Code: Select all

HotIfwinActive target_win
Hotkey "~LButton", copyCode
Hotkey "~Up", copyCode
Hotkey "~Down" copyCode
Hotkey "~Left" copyCode
Hotkey "~Right" copyCode

copyCode(thisHotkey) {
;basically a function that sends ^c
;manages clipboard data 
; then checks and returns data from my sqlite DB.
; as you can see this is not a very light process task.
}
currently navigating with the arrow keys queue up many of the same function, i would like to debounce them, something like, if I accessed the same function 10 times and less than X ms has passed, prevent all previous tasks and just do the last one.

Is something like this possible?
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: A way to debounce hotkeys

18 Dec 2023, 19:47

1. A variable can serve as a counter.
2. Elapsed time can be measured via A_TickCount.
3. Aborting can be achieved by restarting the script.
4. When a script is run, command-line parameters can be added to indicate that the script should execute a specific task.
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: A way to debounce hotkeys

19 Dec 2023, 03:30

mikeyww wrote:
18 Dec 2023, 19:47
1. A variable can serve as a counter.
2. Elapsed time can be measured via A_TickCount.
3. Aborting can be achieved by restarting the script.
4. When a script is run, command-line parameters can be added to indicate that the script should execute a specific task.
I know I can use a variable to keep track of A_TickCount, I was thinking if somebody already created something like this so i dont have to reinvent the wheel, and I dont see what the use of point 3 and 4 is to my problem.

I will try to see if I can cook something with A_PriorHotkey and A_TickCount.
niCode
Posts: 295
Joined: 17 Oct 2022, 22:09

Re: A way to debounce hotkeys

19 Dec 2023, 06:19

fenchai wrote: I dont see what the use of point 3 and 4 is to my problem.
You can't just quit a function any time you want. The purpose of point 3 is to forcefully end the functions running.

As for point 4, (this is actually the first time I've played around with this), which is actually combined with point 3 since it includes reloading the script, this allows you to restart the script and conditionally do something.

Take a look at the following test script:

Code: Select all

#SingleInstance

if A_Args.Length and A_Args[1] = 'RunFunction'
    MyFunc()

F1::Run('AutoHotkey.exe /restart ' A_ScriptName ' ' 'RunFunction')
F2::Reload

MyFunc() {
    MsgBox('nice')
}
Whether you're running it the first time, reloading it from task tray, or reloading it with F2, MyFunc doesn't run. It will only run when reloading the script and passing a parameter specified by my choosing ('RunFunction') by pressing the F1 hotkey. This approach would allow you to stop all instances of the function from running (by reloading the script) and calling the copyCode function when the script starts again.
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: A way to debounce hotkeys

19 Dec 2023, 06:43

Another example of this is below.

Code: Select all

#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 9
targetWindow := 'ahk_exe notepad.exe'
nMax         := 3
reset
If A_Args.Length     ; If a command-line parameter was provided,
 copyCode(A_Args[1]) ;  then call the function
SoundBeep 1500

#HotIf WinActive(targetWindow)
~LButton::
~Up::
~Down::
~Left::
~Right:: {
 Global n := n + 1
 SetTimer reset, -1000
 ToolTip n
 If n > nMax                                                   ; If threshold is exceeded,
  Run A_AhkPath ' /restart "' A_ScriptFullPath '" ' ThisHotkey ;  then restart the script
 Else copyCode(ThisHotkey)
}
#HotIf

reset() {
 Global n := 0
 ToolTip
}

copyCode(hk) {
 Sleep 500
 SendText hk
}
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: A way to debounce hotkeys

19 Dec 2023, 08:18

mikeyww wrote:
19 Dec 2023, 06:43
Another example of this is below.

Code: Select all

#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 9
targetWindow := 'ahk_exe notepad.exe'
nMax         := 3
reset
If A_Args.Length     ; If a command-line parameter was provided,
 copyCode(A_Args[1]) ;  then call the function
SoundBeep 1500

#HotIf WinActive(targetWindow)
~LButton::
~Up::
~Down::
~Left::
~Right:: {
 Global n := n + 1
 SetTimer reset, -1000
 ToolTip n
 If n > nMax                                                   ; If threshold is exceeded,
  Run A_AhkPath ' /restart "' A_ScriptFullPath '" ' ThisHotkey ;  then restart the script
 Else copyCode(ThisHotkey)
}
#HotIf

reset() {
 Global n := 0
 ToolTip
}

copyCode(hk) {
 Sleep 500
 SendText hk
}
thanks for giving me a working example, I shall try it when i have the time.

I would like to ask, what is the point of this line

Code: Select all

Run A_AhkPath ' /restart "' A_ScriptFullPath '" ' ThisHotkey
? does reload not do the same?
also ThisHotkey what is its purpose? on my precious code I had to add this as parameter of the func because of the new ahk v2 syntax (I think I could do (*)=> to omit the parameter) but I am not sure what is the purpose in your example.

also my script shows a gui, this way of forcefully restarting the script im not sure if its visually pleasing 😅, but i did encounter the maxhotkeysperthread issue when using my previous hotkey method, I then added $~LButton:: and it worked but I think this method was preventing my keyboard presses (it was buggy) so I removed the $.
I will continue to test and report. thanks for the guidance.
User avatar
mikeyww
Posts: 26977
Joined: 09 Sep 2014, 18:38

Re: A way to debounce hotkeys

19 Dec 2023, 08:46

The tilde is not a filler. It enables the hotkey's native function.

Restarting the script with a command-line parameter is a way to instruct the script to call your function.

You may use the hotkey in your function, or not use it. That is up to you.

Mouse hotkeys automatically use the mouse hook.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 20 guests