Page 1 of 1

PressNHold [Function]

Posted: 03 Sep 2017, 11:09
by Delta Pythagorean
Ok, since the community get's asked this so many times, I might as well make a simple and easy to use Press and Hold script.

Here's the function with an example:

Code: Select all

F1::PressNHold(A_ThisHotkey, "{space}", 10)

PressNHold(Key, Press := "", Delay := 100) {
	While (GetKeyState(Key, "P")) {
		SendInput % (Press) ? Press : Key
		Sleep % Delay
	}
}
Explaination:
This allows, when F1 is pressed, to "spam" the space key with a delay of 10 milliseconds.
To do multiple keys, do as follows:

Code: Select all

F1::PressNHold(A_ThisHotkey, "ab", 10)

PressNHold(Key, Press := "", Delay := 100) {
	While (GetKeyState(Key, "P")) {
		SendInput % (Press) ? Press : Key
		Sleep % Delay
	}
}
Explanation:
Upon pressing F1, again, will "spam" ab repeatedly until you let go of F1.

Any questions can be asked here and if you need anything, I'll be having fun under the sun. (Not really because I never go outside :P)

Re: PressNHold [Function]

Posted: 03 Sep 2017, 11:38
by waetherman
How to make it work with modifier keys, e.g. ~F1?
Maybe something like this would work?

Code: Select all

RegExMatch( Key, "[!+#^~*]*+(.*)", realKey )
While (GetKeyState(realKey1, "P")) {

Re: PressNHold [Function]

Posted: 03 Sep 2017, 11:50
by Delta Pythagorean
waetherman wrote:How to make it work with modifier keys, e.g. ~F1?
Maybe something like this would work?

Code: Select all

RegExMatch( Key, "[!+#^~*]*+(.*)", realKey )
While (GetKeyState(realKey1, "P")) {
Something close to that I'd imagine.
You could do RegExReplace and return the string directly.
There's many ways where I could make it a lot easier, but right now, I need to relax. Haven't got vacation yet.

Re: PressNHold [Function]

Posted: 13 Dec 2017, 10:38
by Reloaded
Delta Pythagorean wrote:Ok, since the community get's asked this so many times, I might as well make a simple and easy to use Press and Hold script.

Here's the function with an example:

Code: Select all

F1::PressNHold(A_ThisHotkey, "{space}", 10)

PressNHold(Key, Press := "", Delay := 100) {
	While (GetKeyState(Key, "P")) {
		SendInput % (Press) ? Press : Key
		Sleep % Delay
	}
}
Explaination:
This allows, when F1 is pressed, to "spam" the space key with a delay of 10 milliseconds.
To do multiple keys, do as follows:

Code: Select all

F1::PressNHold(A_ThisHotkey, "ab", 10)

PressNHold(Key, Press := "", Delay := 100) {
	While (GetKeyState(Key, "P")) {
		SendInput % (Press) ? Press : Key
		Sleep % Delay
	}
}
Explanation:
Upon pressing F1, again, will "spam" ab repeatedly until you let go of F1.

Any questions can be asked here and if you need anything, I'll be having fun under the sun. (Not really because I never go outside :P)
Awesome Script!, i really like it :)