0
votes

I have an ahk script which types a series of keys. It works up until the last key. When it comes to the last key, the AHK script just seems to hold down the key until I press another key manually or exit the script (which I don't want to do).

#SingleInstance force
Sleep, 3000
Send {r}
Sleep, 100
Send {u}
Sleep, 100
Send {n}
Sleep, 100
Exit

I mean, its a straight forward script but instead of seeing "run" I get "runnnnnnnnnnnnnnn(... to infinity)". It should be noted that if I make the sleep command longer, each key will be held down for that amount of time. 100 seconds ensures I only get the character once (except the last key) before moving on to the next.

How can I guarantee that the script will only press each key once?

2
Could you please provide the rest of your script for reference? There does not appear to be anything wrong with the snippett you have posted that would have caused that looping behavior - Spyre
That's literally the entire script - Sweepster
Interesting... Would you mind describing what you are trying to do with your script? At the present, it is effectively the same thing as Send run if you ignore the Sleeps - Spyre
I just want the script to wait 3 seconds and type "run" instead of "runnnnnnnnnnnn...." - Sweepster

2 Answers

0
votes

I see nothing wrong with your use of the method, but your keyboard, lag/latency, and the specific program you're sending keys to can effect the reliability of the Send method. Clearly, your keyboard is not releasing the key at all, since you have to trim the Sleep to 100ms to ensure a single press. I use this custom KeyPress() method to help ensure keypresses in finicky programs. It sends the "press" and "release" portions of the keypress on different ticks. Sleeping by 1 ms ensures at least 1 tick will pass between commands.

Note that for this to work, you must pass a literal string to the Keypress() method. Hence, you must send the key you wish to press enclosed in double quotation marks

#SingleInstance force

F10::
{
    KeyPress("r")
    KeyPress("u")
    KeyPress("n")
}


KeyPress(Key)
{
    Send, {%Key% down}
    Sleep 1
    Send, {%Key% up}
    Sleep 1
}

0
votes

Responding to the comment stating that the X of the XY Problem is that OP wants a script that will Delay 3 seconds before sending "run".

#SingleInstance force
Sleep, 3000
SendInput run ;SendInput is more reliable than Send when trying to send plaintext to programs