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.
}
}
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?