Preventing continuous inputs when holding down key Topic is solved

Ask gaming related questions
Pure1108
Posts: 7
Joined: 31 May 2022, 06:43

Preventing continuous inputs when holding down key

Post by Pure1108 » 23 Mar 2023, 23:34

By default, holding down a key on my keyboard will send an initial input, and then continuously repeat the key after a short delay.

How can I write a script to ensure that one keyboard press always sends one input, regardless of whether the key is held down or pressed once.

In other words, holding ‘p’ would just send one ‘p’ until the key is released.

I’ve been looking through the API hoping to figure it out instead of making a post, and the KeyWait function seems like a possible solution but unfortunately that’s as far as I got (I’m new to AHK).

I also wasn’t sure how to write a function to apply to every key on the keyboard without manually listing all the hot keys in the AHK script.

Hopefully that makes sense, I would be really grateful for any help please

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

Re: Preventing continuous inputs when holding down key

Post by mikeyww » 24 Mar 2023, 06:45

One idea is below. It might need adjustments, especially to handle LWin.

Code: Select all

; This script prevents keys from repeating when held
#Requires AutoHotkey v2.0
waitForKeyRelease := (ThisHotkey) => (Send('{Blind}{' (key := SubStr(ThisHotkey, 2)) '}'), KeyWait(key))

; Adapted from Rohwedder: https://www.autohotkey.com/boards/viewtopic.php?p=511532#p511532
For each, LF in [[0x1FF, 'sc{:X}'], [0xFF, 'vk{:X}']]
 Loop LF[1]
  ('' != Key := GetKeyName(Format(LF[2], A_Index))) && Hotkey('*' Key, waitForKeyRelease, 'On')

Pure1108
Posts: 7
Joined: 31 May 2022, 06:43

Re: Preventing continuous inputs when holding down key

Post by Pure1108 » 24 Mar 2023, 07:29

Thanks so much @mikeyww for the quick and helpful reply - the script seems almost perfect, how would I go about making the following changes please? (I did have a go myself but it didn't end well lol):

1. Mouse wheel scrolling seems to be disabled when the script is running (the mouse wheel button still works)
2. The script also prevents holding down mouse buttons (can no longer click and drag with left mouse button)
3. Make an exemption for space bar

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

Re: Preventing continuous inputs when holding down key

Post by mikeyww » 24 Mar 2023, 19:03

What does the loop currently do?

1. Gets the key name
2. Checks to see if the key name is null
3. If not null, it calls the hotkey function.

A way to alter:

1. Expand the && statement into an If.... Else statement instead: if the key name is not null, then call the hotkey function.
2. Add more conditions: execute the hotkey command only if the key name does not match any of your exclusions.

To find out what the key names actually are, you can have the script display or log them as the loop iterates.

Pure1108
Posts: 7
Joined: 31 May 2022, 06:43

Re: Preventing continuous inputs when holding down key

Post by Pure1108 » 25 Mar 2023, 05:32

Thanks for the reply I've been looking into making an If, else statement - please could you just explain the below line because I couldn't work it out

Code: Select all

For each, LF in [[0x1FF, 'sc{:X}'], [0xFF, 'vk{:X}']]
I also struggled to find information on logging keys on the forums - it seems people don't want to talk about it due to the potential of using it maliciously - :shifty:

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

Re: Preventing continuous inputs when holding down key  Topic is solved

Post by mikeyww » 25 Mar 2023, 06:51

Below is one example of how to exclude some keys.

Code: Select all

; This script prevents keys from repeating when held
#Requires AutoHotkey v2.0
waitForKeyRelease := (ThisHotkey) => (Send('{Blind}{' (key := SubStr(ThisHotkey, 2)) '}'), KeyWait(key))
exclude           := '(Control|Alt|Shift|Win|Button|Wheel|Space)' ; Regular expression

; Adapted from Rohwedder: https://www.autohotkey.com/boards/viewtopic.php?p=511532#p511532
For each, LF in [[0x1FF, 'sc{:X}'], [0xFF, 'vk{:X}']]
 Loop LF[1]
  If !((key := GetKeyName(Format(LF[2], A_Index))) ~= exclude)
   Hotkey '*' key, waitForKeyRelease, 'On'
The line that you mentioned shows an array of arrays. An array is an object that is a set of key-value pairs. In a simple array, only the values are shown in the code. The key is an integer representing the element number in the array. The For function calls the object's _NewEnum() method, to retrieve an enumerator object. In other words, it loops through each array element. This enables a block of statements to be executed for each key-value pair in the object. You might want to read about arrays in the AHK documentation.

In this script, the array of arrays is used so that a large set of both scan codes (SC) and virtual key codes (VK) can be used with the Hotkey function, which then calls the function that is defined in the script. The For command in this script has two iterations. Each iteration yields an array as LF. LF[1] is the first element of that array. This is a number used in the Loop function call. The number is shown in hexadecimal format, but it could instead be written in decimal format. LF[2] is the text that is used in the Format function, to format the number so that the GetKeyName function will work properly.

For the exclusions that have been added, the new hotkey will be added only if the RegExMatch is zero. In other words, if there is a match to any of the exclusions, the hotkey function call will be skipped. For the regular expression shown, a match occurs if any of the case-sensitive substrings is contained within the key name.

Below is a shorthand way to combine the conditional statement and the Hotkey function call.

Code: Select all

((key := GetKeyName(Format(LF[2], A_Index))) ~= exclude) || Hotkey('*' key, waitForKeyRelease, 'On')

Pure1108
Posts: 7
Joined: 31 May 2022, 06:43

Re: Preventing continuous inputs when holding down key

Post by Pure1108 » 25 Mar 2023, 12:08

Thank you so much for all your help and explanations, I really appreciate it so much :dance:

Post Reply

Return to “Gaming”