PressNHold [Function]

Post gaming related scripts
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

PressNHold [Function]

03 Sep 2017, 11:09

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)
User avatar
waetherman
Posts: 112
Joined: 05 Feb 2016, 17:00

Re: PressNHold [Function]

03 Sep 2017, 11:38

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")) {
Image
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: PressNHold [Function]

03 Sep 2017, 11:50

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.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
Reloaded
Posts: 283
Joined: 25 Aug 2017, 08:48

Re: PressNHold [Function]

13 Dec 2017, 10:38

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 :)

Return to “Gaming Scripts (v1)”

Who is online

Users browsing this forum: No registered users and 25 guests