I have a keyboard with these arrow (less/greater than) characters as alternate keys on Y and X.
Let's focus on the key X for this example.
By default the alternate character > is triggered with AltGr + X of course.
But I would like to trigger it by simply long pressing X, to speed things up and having no need for a second hand or finger.
So far I have the following, which I got from other posts:
$x::
KeyWait, x, T0.2
if (ErrorLevel)
Send > ;long
else {
KeyWait, x, D T0.2
if (ErrorLevel)
Send x ;single
}
KeyWait, x
return
This basically works, but has one major flaw: A normal single key press takes now too much time to write the normal X character.
For instance, if you write fast a word like "exchange", you end up with something like "echxange", because the X takes too much time to be sent.
So how can this script be modified to fix that problem? My idea would be to send a normal X and to abord this whole script, once a {X Up} is registered. So after {X Up} he will no longer wait.
Or any other idea?
Thanks.
To just press (hold down) or release the key, follow the key name with the word "down" or "up" as shown below. Note: As capital letters are produced by sending Shift , A produces a different effect in some programs than a . For example, ! A presses Alt + Shift + A and !a presses Alt + A .
To repeat a keystroke: Enclose in braces the name of the key followed by the number of times to repeat it. For example: Send "{DEL 4}" ; Presses the Delete key 4 times. Send "{S 30}" ; Sends 30 uppercase S characters.
AutoHotkey is a free and open-source program which uses its own scripting language to automate any of your daily Windows tasks. Even though the “scripting language” sounds intimidating, it is very easy to use, and you can do all sorts of cool things.
Here is a solution that calculates the duration of the keypress, the downside is you always have to release the key in order to get the required input. This means you can't hold down x to type xxxxxxxx.
$x::
startTime := A_TickCount ;record the time the key was pressed
KeyWait, x, U ;wait for the key to be released
keypressDuration := A_TickCount-startTime ;calculate the duration the key was pressed down
if (keypressDuration > 200) ;if the key was pressed down for more than 200ms send >
{
Send >
}
else ;if the key was pressed down for less than 200ms send x
{
Send x
}
return
I took the answer provided by Yane, and improved it slightly.
The difference: Yane's answer will send the key when you release the key. My example will send the long-hold key after a set amount of time. This way you know when you've hold the key long enough.
$x::
timeHasElapsed := 0
SetTimer, SendAngleBracketRight, -200 ;if the key was pressed down for more than 200ms send > (negative value to make the timer run only once)
KeyWait, x, U ;wait for the key to be released
if (timeHasElapsed == 0) ;if the timer didn't go off disable the timer and send x
{
SetTimer, SendAngleBracketRight, OFF
SendInput, x
}
return
SendAngleBracketRight:
SendInput, >
timeHasElapsed := 1
return
The code below seems to work well for me, but note that there is the possibility (with some unfortunate timing) that it could erroneously replace an "x" if you're typing many x's in a row, resulting in something like this, "xxxx>xxx". I increased the sleep time to 350ms to make that scenario less likely, but it's something to be aware of and should be changed to suit whatever you need.
~x::
Sleep , 350
BlockInput , On
Send , % GetKeyState( "x" , P ) ? "{backspace}>" : ""
KeyWait , x , U
BlockInput , Off
Return
Note that since it sends a backspace, you may get unintended results if using it in a non-typing environment, such as in a browser where it may go back to the previous page if you can't type text. This can be mitigated if needed by filtering out those specific scenarios with WinActive
or something similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With