PressNHold [Function]

Post gaming related scripts
User avatar
Delta Pythagorean
Posts: 304
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Equestria

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] [Download] (PressNHold.ahk)GeSHi © Codebox Plus

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] [Download] (PressNHold.ahk)GeSHi © Codebox Plus

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)
waetherman
Posts: 82
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] [Download] GeSHi © Codebox Plus

RegExMatch( Key, "[!+#^~*]*+(.*)", realKey )
While (GetKeyState(realKey1, "P")) {
Check out my cool intrusiveCALC!
User avatar
Delta Pythagorean
Posts: 304
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Equestria

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] [Download] GeSHi © Codebox Plus

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.

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 4 guests