Long-press Function keys to control Media buttons Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
iamMG
Posts: 28
Joined: 12 Nov 2017, 11:32

Long-press Function keys to control Media buttons

Post by iamMG » 27 Mar 2024, 07:41

Hi, I've made a function that can perform two tasks. One is when the Hotkey is pressed for a short duration (0.2 second in this case). The other is when it is pressed for more than this duration.

Code: Select all

#requires AutoHotkey v2.0
#singleInstance force
persistent

$F1:: longPress("{Volume_Mute}")
$F3:: longPress("{Volume_Up}" , true)

; "hold := true" to be used when the key needs to be pressed for a continuous action, like increasing/decreasing Volume/brightness, etc.

longPress(action , hold := false) {
    mainKey := SubStr(A_ThisHotkey , 2)		; to remove the modifier "$".
	if KeyWait(mainKey, "T0.2")				; handles short-press to perform native assigned function.
		Send("{" mainKey "}")
	else {									; handles long-press to perform a given new task.
        if !hold {							; handles keys which don't require a continuous press. Does the task once and exits the conditions.
            Send(action)
        } else while GetKeyState(mainKey , "P") {	; handles keys which can perform task on a continuous press. Continues doing the task ...
            Send(action)							; ... until the key is physically released and then exits the conditions.
            Sleep(50)						; for smoother performance.
        }
        keyWait(mainKey)					; in any case wait for the release of the key before exiting completely.
    }
}
One should download the script and run it in a browser, like Chrome. Short-pressing F1 will open help window. Long-press will mute. This works perfectly.
However, the "continuous press" part of the script is giving unpredictable results. Pressing and holding F3 key sometimes pops up a Find window while increasing volume.

Can anyone optimize this code or figure out what might be causing the issue?

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

Re: Long-press Function keys to control Media buttons  Topic is solved

Post by mikeyww » 27 Mar 2024, 08:41

You might want to have an unconditional KeyWait at the end of your function.

This forum has oodles of tap-hold scripts in case any of them give you additional tips!

iamMG
Posts: 28
Joined: 12 Nov 2017, 11:32

Re: Long-press Function keys to control Media buttons

Post by iamMG » 27 Mar 2024, 09:12

mikeyww wrote:
27 Mar 2024, 08:41
You might want to have an unconditional KeyWait at the end of your function.
Wow, that did it. Haha.
mikeyww wrote:
27 Mar 2024, 08:41
This forum has oodles of tap-hold scripts in case any of them give you additional tips!
Thanks, I found out about TapHoldManager. Do you reckon if any other script/library is better than this one?

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

Re: Long-press Function keys to control Media buttons

Post by mikeyww » 27 Mar 2024, 10:16

I have not tested it, but it seems to be popular.

Post Reply

Return to “Ask for Help (v2)”