VOGONS

Common searches


First post, by robertmo3

User metadata
Rank Member
Rank
Member

Do you know any software allowing "toggle hold" (keep pressed) keyboard keys for modern windows?

Reply 1 of 5, by darry

User metadata
Rank l33t++
Rank
l33t++
robertmo3 wrote on 2024-02-24, 09:42:

Do you know any software allowing "toggle hold" (keep pressed) keyboard keys for modern windows?

DISCLAIMER : I am not a programmer, I just found some code on the Internet, tweaked it and and tested it under Windows 10. Running this under newer Windows 10 or 11 builds may require admin privileges or may no longer work at all (I would guess). It works for me.

Based on [1], this powershell script [2], invoked with [3] (assuming you name the script contkeypress.ps1 ), would do it .
I believe that the value passed to "Start-Sleep -milliseconds " essentially simulates the typematic rate, but I am not sure.
Adjust variables as needed.

[1]
https://gist.github.com/jamesfreeman959/231b0 … 675f21c0e346a9c

[2]
The powershell script:

param([int]$seconds=10)  # input param, 10 seconds if no value provided

$wsh = New-Object -ComObject WScript.Shell
$a = Get-Date # Current time
$b = $a.AddSeconds($seconds) # Target time

while ($a -lt $b) {
# press a continuously at given intervals, '+{a}' would press Shift+a , for example
$wsh.SendKeys('{a}')
Start-Sleep -milliseconds 0 # sleep for 0 milliseconds
$a = Get-Date
}

[3]
powershell.exe -file contkeypress.ps1 -seconds 5

Reply 2 of 5, by doshea

User metadata
Rank Member
Rank
Member

That script looks like it hits the "a" key as many times as it can for 10 seconds, rather than toggling it on and off based on the user hitting a key. I think it would take some work to make it read a key and then repeat that key until the key is hit again, which I assume is what the OP wanted?

Also, that's more like sending a sequence like:

send "a" key press
send "a" key release
send "a" key press
send "a" key release

really fast, whereas perhaps what OP wants is:

send "a" key press
wait until real "a" key is pressed
send "a" key release

AutoHotkey can definitely let you send just a key down event and then send a key up event later, see e.g. https://www.autohotkey.com/docs/v2/lib/Send.htm I wasn't able to find the documentation for SendKeys in WScript.Shell (I don't know much about PowerShell but it seems weird to me that it'd be hard to find the documentation) but from looking at other SendKeys documentation from Microsoft, it doesn't seem to let you send down and up events separately.

A Google search for "autohotkey toggle key" seems to have plenty of results that do various things, perhaps one of them does what OP wants.

I have no idea if AutoHotkey still works in modern Windows, as I don't use modern Windows 😁

OP, I imagine that AutoHotkey might be able to do what you asked for in your other thread too.

Reply 3 of 5, by darry

User metadata
Rank l33t++
Rank
l33t++
doshea wrote on 2024-03-02, 10:05:
That script looks like it hits the "a" key as many times as it can for 10 seconds, rather than toggling it on and off based on t […]
Show full quote

That script looks like it hits the "a" key as many times as it can for 10 seconds, rather than toggling it on and off based on the user hitting a key. I think it would take some work to make it read a key and then repeat that key until the key is hit again, which I assume is what the OP wanted?

Also, that's more like sending a sequence like:

send "a" key press
send "a" key release
send "a" key press
send "a" key release

really fast, whereas perhaps what OP wants is:

send "a" key press
wait until real "a" key is pressed
send "a" key release

AutoHotkey can definitely let you send just a key down event and then send a key up event later, see e.g. https://www.autohotkey.com/docs/v2/lib/Send.htm I wasn't able to find the documentation for SendKeys in WScript.Shell (I don't know much about PowerShell but it seems weird to me that it'd be hard to find the documentation) but from looking at other SendKeys documentation from Microsoft, it doesn't seem to let you send down and up events separately.

A Google search for "autohotkey toggle key" seems to have plenty of results that do various things, perhaps one of them does what OP wants.

I have no idea if AutoHotkey still works in modern Windows, as I don't use modern Windows 😁

OP, I imagine that AutoHotkey might be able to do what you asked for in your other thread too.

I searched for a while to try to determine whether there was a distinction between a continuous keypress and successive, very short interval keypresses in the context of Windows and I could not find any information that even mentioned a continuous keypress. At this point, I am not sure that there is a clear distinction between the two [1]. In fact, I'm pretty much starting to believe that a continuous keypress is not a concept that exists in the Windows world (I could be wrong, of course).

[1]
https://www.pcmag.com/encyclopedia/term/typem … and%20the%20Mac.

Reply 4 of 5, by robertmo3

User metadata
Rank Member
Rank
Member

Could you rewrite the source so that:
- it doesn't use letter "a" as a parameter,
- it doesn't use letter "a" as an example,
- it doesn't use the same letter for a parameter and an example.

So for example:
- parameters start from letter "b", etc.,
- letter example for being pressed is "z".

Reply 5 of 5, by doshea

User metadata
Rank Member
Rank
Member
darry wrote on 2024-03-03, 05:26:

I searched for a while to try to determine whether there was a distinction between a continuous keypress and successive, very short interval keypresses in the context of Windows and I could not find any information that even mentioned a continuous keypress. At this point, I am not sure that there is a clear distinction between the two [1]. In fact, I'm pretty much starting to believe that a continuous keypress is not a concept that exists in the Windows world (I could be wrong, of course).

Maybe it depends on how you look at it but from my perspective it's a concept that has existed since at least Windows 3.x.

Whether it's used depends on the application. Something like Notepad would most likely just accept WM_CHAR events to find out that the user wants a letter to be inserted into their file; if you hold down a key, Windows will generate repeated WM_CHAR events until you release it (based on your typematic settings) and Notepad will just keep receiving the letter over and over.

Something like a game is likely to process WM_KEYDOWN and WM_KEYUP events instead - at least some of the time - so that you can for example keep moving while the key is held down, rather than just moving a little bit every time the key repeats. As an example/evidence: https://github.com/dcoshea/NewWolfLives/blob/ … /vid_win.c#L233

I would imagine that in a modern first-person shooter game for example, if you just sent repeated "w" key down and up events you might not move as fast as if you sent a "w" key down event and left it down for a while, because when the game actually processes a frame, the key is just as likely to be up as it is to be down, right? It might depend on how the game is implemented though.

robertmo3 wrote on 2024-03-03, 06:16:
Could you rewrite the source so that: - it doesn't use letter "a" as a parameter, - it doesn't use letter "a" as an example, - i […]
Show full quote

Could you rewrite the source so that:
- it doesn't use letter "a" as a parameter,
- it doesn't use letter "a" as an example,
- it doesn't use the same letter for a parameter and an example.

So for example:
- parameters start from letter "b", etc.,
- letter example for being pressed is "z".

Yes, that would certainly be possible, and preferable - $a and $b are pretty bad variable names, we can do much better 😁 (and yes I acknowledge that you said you're not a programmer darry so I don't blame you!)

I don't really know PowerShell and can't easily test this, but hopefully this is a little clearer and actually works (I assume my variable names should be valid for PowerShell as they'd be valid for just about any other language from the last few decades):

param([int]$seconds=10)  # input param, 10 seconds if no value provided

$wsh = New-Object -ComObject WScript.Shell
$now = Get-Date # Current time
$release_time = $now.AddSeconds($seconds) # Target time

while ($now -lt $release_time) {
# press a continuously at given intervals, '+{z}' would press Shift+z , for example
$wsh.SendKeys('{x}')
Start-Sleep -milliseconds 0 # sleep for 0 milliseconds
$now = Get-Date
}