# what am I doing wrong?

## Recommended Posts

What I would like is for this to slowly press 4 3 2 1 and pause when I take my finger off of 2 and restart from where it stopped. So hold 2 and get 4 3 2 and if I let up on 2 it waits and then goes to 1 4 3... when I press 2 again. Not sure where im screwing up.

```Do
If _IsPressed("32") = 1 then
Send("{4}")
Sleep(Random(1500,1670,1))
Elseif _IsPressed("32") = 0 then
Do
Sleep(100)
Until _isPressed("32") = 1
EndIf
If _IsPressed("32") = 1 then
Send("{3}")
Sleep(Random(1500,1670,1))
Elseif _IsPressed("32") = 0 then
Do
Sleep(100)
Until _isPressed("32") = 1
EndIf
If _IsPressed("32") = 1 then
Send("{2}")
Sleep(Random(1500,1670,1))
Elseif _IsPressed("32") = 0 then
Do
Sleep(100)
Until _isPressed("32") = 1
EndIf
If _IsPressed("32") = 1 then
Send("{1}")
Sleep(Random(1500,1670,1))
Elseif _IsPressed("32") = 0 then
Do
Sleep(100)
Until _isPressed("32") = 1
EndIf
Until _IsPressed("23")
Exit```

##### Share on other sites

Not sure I understand... start from the very beginning and map out the logic you wish this to perform.

At what point do you press '2' ?

Why the random sleep patterns?

what is the ultimate goal you wish to perform?

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

I would take a completely different approach. Have a single loop, and then variables for the current number, the amount of time to wait, the timer and whether the key is pressed or not.

There are 4 conditions to work with: Whether the key is currently pressed or not, and whether it was pressed before or not (so a combination of _IsPressed and your \$fPressed variable). This equates to the 4 "events": Key pressed, Key released, Key still pressed and key still released.

On those 4 events its pretty simple to see what needs doing. If the key is pressed, you stop the timer, and set \$fPressed. When the key is released you restart the timer and unset \$fPressed. When the key is still pressed you do nothing, and when the key is still released you check the timer against your wait time, and if its complete you print the number, reset the timer and move onto the next.

Timers in AutoIt require a bit of thinking to "pause" and "reset". For reset you make a new one, and for pause you take the current time difference off your wait time.

```Local \$iNumber
Local \$iTimer
Local \$iWait
Local \$fPressed

While 1
If \$fPressed Then
If _IsPressed() Then ; Key still pressed
Else ; Key is released
EndIf
Else
If _IsPressed() Then ; Key is pressed
Else ; Key is still released
If TimerDiff() > \$iWait Then
; Timer has finished!
EndIf
EndIf
EndIf
WEnd```

##### Share on other sites

Also you are missing the _ispressed function, unless that is just a snippet of a much larger code.

Try taking what you have, and integrate

```#include [/size]

Local \$hDLL = DllOpen("user32.dll")

While 1
If _IsPressed("10", \$hDLL) Then
ConsoleWrite("_IsPressed - Shift Key was pressed." & @CRLF)
; Wait until key is released.
While _IsPressed("10", \$hDLL)
Sleep(250)
WEnd
ConsoleWrite("_IsPressed - Shift Key was released." & @CRLF)
ElseIf _IsPressed("1B", \$hDLL) Then
MsgBox(0, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.")
ExitLoop
EndIf
Sleep(250)
WEnd
[size=4]DllClose(\$hDLL)```
it into this:

C0d3 is P0etry( ͡° ͜ʖ ͡°)

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...