Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to get the following behavior with AutoHotKey: When the A key is pressed, AHK holds down the S key until the D key is pressed. The following script isn't working as expected:

a::
    Send {s Down}
    return

d::
    if (GetKeyState("s", "P"))
        {
        Send {s Up}
        }
    return

Neither is the following:

a::
    release_s = 0
    Loop
        {
        SendInput, s
        if release_s
            break
        }
    return

d::
    release_s = 1
    return
share|improve this question

2 Answers 2

Sending {s Down} will not cause repetition of a key. You need to use a loop. Give this a try:

a::
    stop = 0
    Loop 
    {
        SendInput, s
        Sleep 50 ;adjust for speed of repetition
        if stop
            break
    }
    return


d::
    stop = 1
    return
share|improve this answer
    
Repetition? What repetition? If I hold down a key on my keyboard, I'm holding it down, not repeating anything. The moment I press it my keyboard tells the computer "key X is down" and the moment I depress it my keyboard says "key X is up". Am I missing something? –  Core Xii Nov 12 '12 at 1:17
    
Directly from the documentation: When a key is held down via the method above, it does not begin auto-repeating like it would if you were physically holding it down (this is because auto-repeat is a driver/hardware feature). However, a Loop can be used to simulate auto-repeat. autohotkey.com/docs/commands/Send.htm –  Elliot DeNolf Nov 12 '12 at 16:17
    
But I don't want auto-repeat. I just want the key to be held down. Think about a video game, for example: holding 'left' makes the player character move left for as long as you're holding it. There's no auto-repeat happening; the key is either down or it's up. –  Core Xii Nov 13 '12 at 2:06
    
You can achieve this by removing the Sleep 50 line. There is now no delay between the sendinputs and therefore simulates being held down. If this does not work for you, I'm out of ideas. Best of luck. –  Elliot DeNolf Nov 13 '12 at 21:11
    
Nope, no avail. The keys do nothing and then AHK complains about too many hotkeys. –  Core Xii Nov 16 '12 at 16:32

Try this one:

a::Send, {s down}

d::
    if(GetKeyState("s")) {
        Send, {s up}
    }
return

Problem in your code:

GetKeyState("s", "P") will only account for physical keys. S on the other has has been sent as a virtual key by AHK.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.