Jump to content

Key Repeat Delay?


Recommended Posts

If you're referring to the rate at which AutoIt sends keys via the Send() command, look up Opt() in the help file index. It allows you to modify the delays involved in holding keys down and inbetween separate keystrokes.

Link to comment
Share on other sites

There's this game and it doesn't react the moment the program sends the key so I need a delay between the repeated key presses.

<{POST_SNAPBACK}>

How about

send("key")
sleep(500)
send("key")

basic but will work :whistle::dance::dance:


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Ok let me specify some more then. This code is for FFR. It looks a bit like this:

HotKeySet("x", "MyExit")
opt("sendKeyDownDelay", 10)

$leftposx = $coord[0]+350
$downposx = $coord[0]+418
$upposx = $coord[0]+484
$rightposx = $coord[0]+551
$posy = $coord[1]+100

$left = pixelgetcolor($leftposx, $posy)
$down = pixelgetcolor($downposx, $posy)
$up = pixelgetcolor($upposx, $posy)
$right = pixelgetcolor($rightposx, $posy)

While 1
 If (pixelgetcolor($leftposx, $posy))<>$left then
  Send("{LEFT}")
 Endif

 If (pixelgetcolor($downposx, $posy))<>$down then
  Send("{DOWN}")
 Endif

 If (pixelgetcolor($upposx, $posy))<>$up then
  Send("{UP}")
 Endif

 If (pixelgetcolor($rightposx, $posy))<>$right then
  Send("{RIGHT}")
 Endif

 Sleep($delay)
WEND

Func MyExit()
 Exit
EndFunc
Now if I increase the sleep in the "While... Wend" loop, you will miss more keys. If you decrease the sleep in the loop, you will press the key too many times (causing "Boos"). There is no perfect number so I wanted to make it so that the program cannot press the same key twice in a row in a certain amount of time.

Link to comment
Share on other sites

Try:

hotkeySet("x", "myExit")
; keys will be pressed down for a quarter of a second now
opt("sendKeyDownDelay", 250)

$leftPosX = $coord[0] + 350
$downPosX = $coord[0] + 418
$upPosX = $coord[0] + 484
$rightPosX = $coord[0] + 551
$PosY = $coord[1] + 100

$left = pixelGetColor($leftPosX, $posY)
$down = pixelGetColor($downPosX, $posY)
$up = pixelGetColor($upPosX, $posY)
$right = pixelGetColor($rightPosX, $posY)

while 1
    $pressed = 0
    if (pixelGetColor($leftPosX, $posY) <> $left) then
        send("{LEFT}")
        $pressed = 1
    endIf
    if (pixelGetColor($downPosX, $posY) <> $down) then
        send("{DOWN}")
        $pressed = 1
    endIf
    if (pixelGetColor($upPosX, $posY) <> $up) then
        send("{UP}")
        $pressed = 1
    endIf
    if (pixelGetColor($rightPosX, $posY) <> $right) then
        send("{RIGHT}")
        $pressed = 1
    endIf
    if not($pressed) then sleep($delay)
wEnd

func myExit()
    exit
endFunc

I introduced a $pressed variable so that the sleep($delay) is only called if no keys were pressed. How does this go for you?

Link to comment
Share on other sites

  • Moderators

I was trying to understand (been up for 26 hours :yawn:) what exactly you were trying to accomplish. I came up w/ 2 ideas...

Even though this is longer, I would probably go w/ this one:

HotKeySet("x", "MyExit")
opt("sendKeyDownDelay", 10)

$leftposx = $coord[0]+350
$downposx = $coord[0]+418
$upposx = $coord[0]+484
$rightposx = $coord[0]+551
$posy = $coord[1]+100

$left = PixelGetColor($leftposx, $posy)
$down = PixelGetColor($downposx, $posy)
$up = PixelGetColor($upposx, $posy)
$right = PixelGetColor($rightposx, $posy)



$LEFT_KEY_PRESS = 0
$DOWN_KEY_PRESS = 0
$UP_KEY_PRESS = 0
$RIGHT_KEY_PRESS = 0

While 1
   Select
      Case (PixelGetColor($leftposx, $posy)) <> $left
         For $i = 1 To 4
            If $i = 1 Then
               If $LEFT_KEY_PRESS < $DOWN_KEY_PRESS Or $LEFT_KEY_PRESS < $UP_KEY_PRESS Or $LEFT_KEY_PRESS < $RIGHT_KEY_PRESS Then
                  Send("{LEFT}")
                  $LEFT_KEY_PRESS = $LEFT_KEY_PRESS + 1
               EndIf
            ElseIf $i = 2 Then
               IF $DOWN_KEY_PRESS < $UP_KEY_PRESS Or $DOWN_KEY_PRESS < $RIGHT_KEY_PRESS Or $DOWN_KEY_PRESS < $LEFT_KEY_PRESS Then
                  Send("{DOWN}")
                  $DOWN_KEY_PRESS = $DOWN_KEY_PRESS + 1
               EndIf
            ElseIf $i = 3 Then
               If $UP_KEY_PRESS < $RIGHT_KEY_PRESS Or $UP_KEY_PRESS < $LEFT_KEY_PRESS Or $UP_KEY_PRESS < $DOWN_KEY_PRESS Then
                  Send("{UP}")
                  $UP_KEY_PRESS = $UP_KEY_PRESS + 1
               EndIf
            Else
               Send("{RIGHT}")
               $RIGHT_KEY_PRESS = $RIGHT_KEY_PRESS + 1
            Endif
         Next
      Case (PixelGetColor($downposx, $posy)) <> $down
         For $i_2 = 1 To 4
            If $i_2 = 1 Then
               IF $DOWN_KEY_PRESS < $UP_KEY_PRESS Or $DOWN_KEY_PRESS < $RIGHT_KEY_PRESS Or $DOWN_KEY_PRESS < $LEFT_KEY_PRESS Then
                  Send("{DOWN}")
                  $DOWN_KEY_PRESS = $DOWN_KEY_PRESS + 1
               EndIf
            ElseIf $i_2 = 2 Then
               If $UP_KEY_PRESS < $RIGHT_KEY_PRESS Or $UP_KEY_PRESS < $LEFT_KEY_PRESS Or $UP_KEY_PRESS < $DOWN_KEY_PRESS Then
                  Send("{UP}")
                  $UP_KEY_PRESS = $UP_KEY_PRESS + 1
               EndIf
            ElseIf $i_2 = 3 Then
               If $RIGHT_KEY_PRESS < $LEFT_KEY_PRESS Or $RIGHT_KEY_PRESS < $DOWN_KEY_PRESS Or $RIGHT_KEY_PRESS < $UP_KEY_PRESS Then
                  Send("{RIGHT}")
                  $RIGHT_KEY_PRESS = $RIGHT_KEY_PRESS + 1
               Endif
            Else
               Send("{LEFT}")
               $LEFT_KEY_PRESS = $LEFT_KEY_PRESS + 1
            Endif
         Next
      Case (PixelGetColor($upposx, $posy)) <> $up
         For $i_3 = 1 To 4
            If $i_3 = 1 Then
               If $UP_KEY_PRESS < $RIGHT_KEY_PRESS Or $UP_KEY_PRESS < $LEFT_KEY_PRESS Or $UP_KEY_PRESS < $DOWN_KEY_PRESS Then
                  Send("{UP}")
                  $UP_KEY_PRESS = $UP_KEY_PRESS + 1
               EndIf
            ElseIf $i_3 = 2 Then
               If $RIGHT_KEY_PRESS < $LEFT_KEY_PRESS Or $RIGHT_KEY_PRESS < $DOWN_KEY_PRESS Or $RIGHT_KEY_PRESS < $UP_KEY_PRESS Then
                  Send("{RIGHT}")
                  $RIGHT_KEY_PRESS = $RIGHT_KEY_PRESS + 1
               EndIf
            ElseIf $i_3 = 2 Then
               If $LEFT_KEY_PRESS < $DOWN_KEY_PRESS Or $LEFT_KEY_PRESS < $UP_KEY_PRESS Or $LEFT_KEY_PRESS < $RIGHT_KEY_PRESS Then
                  Send("{LEFT}")
                  $LEFT_KEY_PRESS = $LEFT_KEY_PRESS + 1
               EndIf
            Else
               Send("{DOWN}")
               $DOWN_KEY_PRESS = $DOWN_KEY_PRESS + 1
            Endif
         Next
      Case (PixelGetColor($rightposx, $posy)) <> $right
         For $i_4 = 1 To 4
            If $i_4 = 1 Then
               If $RIGHT_KEY_PRESS < $LEFT_KEY_PRESS Or $RIGHT_KEY_PRESS < $DOWN_KEY_PRESS Or $RIGHT_KEY_PRESS < $UP_KEY_PRESS Then
                  Send("{RIGHT}")
                  $RIGHT_KEY_PRESS = $RIGHT_KEY_PRESS + 1
               EndIf
            ElseIf $i_4 = 2 Then
               If $LEFT_KEY_PRESS < $DOWN_KEY_PRESS Or $LEFT_KEY_PRESS < $UP_KEY_PRESS Or $LEFT_KEY_PRESS < $RIGHT_KEY_PRESS Then
                  Send("{LEFT}")
                  $LEFT_KEY_PRESS = $LEFT_KEY_PRESS + 1
               EndIf
            ElseIf $i_4 = 3 Then
               IF $DOWN_KEY_PRESS < $UP_KEY_PRESS Or $DOWN_KEY_PRESS < $RIGHT_KEY_PRESS Or $DOWN_KEY_PRESS < $LEFT_KEY_PRESS Then
                  Send("{DOWN}")
                  $DOWN_KEY_PRESS = $DOWN_KEY_PRESS + 1
               EndIf
            Else
               Send("{UP}")
               $UP_KEY_PRESS = $UP_KEY_PRESS + 1
            Endif
         Next
   EndSelect
WEND

Func MyExit()
   Exit
EndFunc;==>MyExit

And this one uses Random... No guarantee that it won't repeat itself... but since it's even numbers... I don't know if the top one won't repeat itself.

HotKeySet("x", "MyExit")
opt("sendKeyDownDelay", 10)

$leftposx = $coord[0]+350
$downposx = $coord[0]+418
$upposx = $coord[0]+484
$rightposx = $coord[0]+551
$posy = $coord[1]+100

$left = PixelGetColor($leftposx, $posy)
$down = PixelGetColor($downposx, $posy)
$up = PixelGetColor($upposx, $posy)
$right = PixelGetColor($rightposx, $posy)





While 1
   Local $RANDOM = Random(1, 4, 1)
   Select
      Case (PixelGetColor($leftposx, $posy)) <> $left
         If $RANDOM = 1 Then
            Send("{LEFT}")
         ElseIf $RANDOM = 2 Then
            Send("{DOWN}")
         ElseIf $RANDOM = 3 Then
            Send("{UP}")
         Else
            Send("{RIGHT}")
         EndIf
      Case (PixelGetColor($downposx, $posy)) <> $down
         If $RANDOM = 1 Then
            Send("{DOWN}")
         ElseIf $RANDOM = 2 Then
            Send("{UP}")
         ElseIf $RANDOM = 3 Then
            Send("{RIGHT}")
         Else
            Send("{LEFT}")
         EndIf
      Case (PixelGetColor($upposx, $posy)) <> $up
         If $RANDOM = 1 Then
            Send("{UP}")
         ElseIf $RANDOM = 2 Then
            Send("{RIGHT}")
         ElseIf $RANDOM = 3 Then
            Send("{LEFT}")
         Else
            Send("{DOWN}")
         Endif
      Case (PixelGetColor($rightposx, $posy)) <> $right
         If $RANDOM = 1 Then
            Send("{RIGHT}")
         ElseIf $RANDOM = 2 Then
            Send("{LEFT}")
         ElseIf $RANDOM = 3 Then
            Send("{DOWN}")
         Else
            Send("{UP}")
         Endif
   EndSelect
WEND

Func MyExit()
   Exit
EndFunc;==>MyExit

I left most of the variables the same... but some things that if you don't have more to your script that are going to give you problems:

$coord[0] & $coord[1] (don't know how your getting the array coords... was assuming either MouseGetPos() or PixelSearch() but neither are present...

Also... there's no Opt("PixelCoordMode", 1 or 2 or 3) So ... I'm assuming your using screen coords.

Anyway... hope it helps or at least gives you some ideas.

GL

EDIT: FIXED A TYPO

EDIT2: HAD - Random(1, 4) / CHANGED TO: Random(1, 4, 1) FOR WHOLE NUMBERS

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

lmao... after thinking about you post... and realizing I may have totally been looking at it wrong... and making a fool of myself... decided to give ya another option....

Cuz I'm Clueless on what you want:

If you want to wait so you don't use the left/right/down/up keys over and over... and the only clue you've given us about this game is the PixelColor... and Assuming there are no other ways of telling that you make an action.

This will Send() your option... then sleep until the pixel doesn't exist in that spot... then it will continue the loop again.

HotKeySet("x", "MyExit")
Opt("SendKeyDownDelay", 10)

$leftposx = $coord[0]+350
$downposx = $coord[0]+418
$upposx = $coord[0]+484
$rightposx = $coord[0]+551
$posy = $coord[1]+100

$left = PixelGetColor($leftposx, $posy)
$down = PixelGetColor($downposx, $posy)
$up = PixelGetColor($upposx, $posy)
$right = PixelGetColor($rightposx, $posy)

While 1
   Select
      Case (PixelGetColor($leftposx, $posy)) == $left
         Send("{LEFT}")
         Do
            Sleep(100)
         Until Not PixelGetColor($leftposx, $posy) == $left
      Case (PixelGetColor($downposx, $posy)) == $down
         Send("{DOWN}")
         Do
            Sleep(100)
         Until Not PixelGetColor($downposx, $posy) == $down
      Case (PixelGetColor($upposx, $posy)) == $up
         Send("{UP}")
         Do
            Sleep(100)
         Until Not PixelGetColor($upposx, $posy) == $up
      Case (PixelGetColor($upposx, $posy)) == $right
         Send("{RIGHT}")
         Do
            Sleep(100)
         Until Not PixelGetColor($rightposx, $posy) == $right
   EndSelect
WEND

Func MyExit()
   Exit
   EndFunc;==>MyExit

EDIT:

ok... 27 hours caught up to me... forgot to take the Local $Random out... I'm off for a nap... VGL 2 you!

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ok let me show you all of my code (which I highly doubt will actually change anything):

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        
;
; Script Function:
; Plays FFR for you!
;
; ----------------------------------------------------------------------------

#include <GUIconstants.au3>

;Activate Window
Msgbox(0,"Setup","Connect to FFR and select your genre before continuing." & chr(13) & "(Have your genre open so the songs can be seen)" & chr(13) & "This program assumes the song is already downloaded and will load almost instantly")
$song=Inputbox("Setup","Song number (1-12)?" & chr(13) & "1 2 3" & chr(13) & "4 5 6" & chr(13) & "7 8 9" & chr(13) & "etc.")
$delay=Inputbox("Setup","Send key delay (default = 125)?")
WinActivate("FlashFlashRevolution.com :: Your Flash Flash Revolution Source - Mozilla Firefox")
WinActivate("FlashFlashRevolution.com :: Your Flash Flash Revolution Source - Microsoft Internet Explorer")

If $song > 13 Then
 MsgBox(0, "FFR", "Please choose a song between 1 and 12.")
 Exit
EndIf

If $song < 1 Then
 MsgBox(0, "FFR", "Please choose a song between 1 and 12.")
 Exit
EndIf

If Not WinActive("FlashFlashRevolution.com :: Your Flash Flash Revolution Source -") Then
 MsgBox(0, "FFR", "Please have FFR open.")
 Exit
EndIf

$coord = PixelSearch( 0, 0, 600, 800, 0xD4E2ED)
If @error Then
 MsgBox(0, "FFR", "Error at finding pixel!")
 Exit
EndIf

;Check Left/Right Mouse Buttons
Dim $primary
Dim $secondary
$k = RegRead("HKEY_CURRENT_USER\Control Panel\Mouse", "SwapMouseButtons")
If $k = 1 Then
$primary = "right"
$secondary = "left"
Else;normal (also case if could not read registry key)
$primary = "left"
$secondary = "right"
EndIf

Select
 Case $Song=1
  MouseClick($primary, $coord[0]+210, $coord[1]+100, 1)
 Case $song=2
  MouseClick($primary, $coord[0]+280, $coord[1]+100, 1)
 Case $song=3
  MouseClick($primary, $coord[0]+350, $coord[1]+100, 1)
 Case $song=4
  MouseClick($primary, $coord[0]+210, $coord[1]+170, 1)
 Case $song=5
  MouseClick($primary, $coord[0]+280, $coord[1]+170, 1)
 Case $song=6
  MouseClick($primary, $coord[0]+350, $coord[1]+170, 1)
 Case $song=7
  MouseClick($primary, $coord[0]+210, $coord[1]+240, 1)
 Case $song=8
  MouseClick($primary, $coord[0]+280, $coord[1]+240, 1)
 Case $song=9
  MouseClick($primary, $coord[0]+350, $coord[1]+240, 1)
 Case $song=10
  MouseClick($primary, $coord[0]+210, $coord[1]+310, 1)
 Case $song=11
  MouseClick($primary, $coord[0]+280, $coord[1]+310, 1)
 Case $song=12
  MouseClick($primary, $coord[0]+350, $coord[1]+310, 1)
 Case $song=13
  MouseClick($primary, $coord[0]+600, $coord[1]+350, 1)
EndSelect

Sleep(2000)
Mousedown($primary)
Sleep(2000)

HotKeySet("x", "MyExit")
opt("sendKeyDownDelay", 10)

$leftposx = $coord[0]+350
$downposx = $coord[0]+418
$upposx = $coord[0]+484
$rightposx = $coord[0]+551
$posy = $coord[1]+100

$left = pixelgetcolor($leftposx, $posy)
$down = pixelgetcolor($downposx, $posy)
$up = pixelgetcolor($upposx, $posy)
$right = pixelgetcolor($rightposx, $posy)

While 1
 If (pixelgetcolor($leftposx, $posy))<>$left then
  Send("{LEFT}")
 Endif

 If (pixelgetcolor($downposx, $posy))<>$down then
  Send("{DOWN}")
 Endif

 If (pixelgetcolor($upposx, $posy))<>$up then
  Send("{UP}")
 Endif

 If (pixelgetcolor($rightposx, $posy))<>$right then
  Send("{RIGHT}")
 Endif

 Sleep(50)
WEND

Func MyExit()
 Exit
EndFunc

Let me state again what I am trying to accomplish (this time with examples). I am trying to make it so that a key cannot press twice within a certain amount of time while other keys can still be pressed. This pressing is from detection of a pixel change. So the way I want this script to work is not to sleep all of the keys but to prevent the pressing of certain keys after they have been pressed within a certain amount of time.

Example: It detects to press "Up" so it sends "{UP}". After it sends it, it still detects it (because the game is slow at responding). So it sends "{UP}" again causing it to lose points. At the same time, it is detecting other keys to be pressed.

Basically I want some way to prevent a key from repeating within a certain amount of time while allowing the other keys to be sent.

Edited by LegacyWeapon
Link to comment
Share on other sites

  • Moderators

Not gonna write out another script :whistle:

You could use the TimerInit() / TimerDiff() probably and seperating them possibly into 4 while statments with exitloops.

GL

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

@sdkano:

Nice hijack.

Send ("{Space 5000}") ; I do not recommend leaving it at 5000

@LegacyWeapon:

I think ronsrules has the right idea on keeping one specific key from being pressed multiple times. Just look up _IsPressed, stick it into a variable and then use the timer ronsules suggested.

Edited by SerialKiller
Link to comment
Share on other sites

  • Moderators

Ok, so I got bored...

$LEFT_0 = 0
$DOWN_0 = 0
$UP_0 = 0
$RIGHT_0 = 0
Dim $LEFT_TIMER , $DOWN_TIMER , $UP_TIMER, $RIGHT_TIMER
Dim $DESIRED_SLEEP_TIME = 10; SECONDS BEFORE KEY SHOULD BE USED AGAIN
While 1
   
   If (pixelgetcolor($leftposx, $posy))<>$left then
      If $LEFT_0 = 0 Then; IF LEFT HAS NOT BEEN USED IN DESIRED TIME, SET TIMER AND USE LEFT
         $LEFT_TIMER = TimerInit() 
         $LEFT_0 = 1; SHOW LEFT HAS BEEN USED AND TIMER HAS BEEN STARTED
         Send("{LEFT}")
      Else
         $LEFT_$DIFFERENCE = TimerDiff($LEFT_TIMER) / 1000
         If $LEFT_$DIFFERENCE >= $DESIRED_SLEEP_TIME Then
            $LEFT_0 = 0; RESET LEFT
         EndIf
      EndIf
   Endif
   
   If (pixelgetcolor($downposx, $posy))<>$down then
      If $DOWN_0 = 0 Then; IF DOWN HAS NOT BEEN USED IN DESIRED TIME, SET TIMER AND USE DOWN
         $DOWN_TIMER = TimerInit()
         $DOWN_0 = 1; SHOW DOWN HAS BEEN USED AND TIMER HAS BEEN STARTED
         Send("{DOWN}")
      Else
         $DOWN_DIFFERENCE = TimerDiff($DOWN_TIMER) / 1000
         If $DOWN_DIFFERENCE >= $DESIRED_SLEEP_TIME Then
            $DOWN_0 = 0; RESET DOWN
         EndIf
      EndIf
   Endif
   
   If (pixelgetcolor($upposx, $posy))<>$up then
      If $UP_0 = 0 Then; IF UP HAS NOT BEEN USED IN DESIRED TIME, SET TIMER AND USE UP
         $UP_TIMER = TimerInit()
         $UP_0 = 1; SHOW UP HAS BEEN USED AND TIMER HAS BEEN STARTED
         Send("{UP}")
      Else
         $UP_DIFFERENCE = TimerDiff($UP_TIMER) / 1000
         If $UP_DIFFERENCE >= $DESIRED_SLEEP_TIME Then
            $UP_0 = 0; RESET UP
         EndIf
      EndIf
   Endif
   
   If (pixelgetcolor($rightposx, $posy))<>$right then
      If $RIGHT_0 = 0 Then; IF RIGHT HAS NOT BEEN USED IN DESIRED TIME, SET TIMER AND USE RIGHT
         $RIGHT_TIMER = TimerInit()
         $RIGHT_0 = 1; SHOW RIGHT HAS BEEN USED AND TIMER HAS STARTED
         Send("{RIGHT}")
      Else
         $RIGHT_DIFFERENCE = TimerDiff($RIGHT_TIMER) / 1000
         If $RIGHT_DIFFERENCE >= $DESIRED_SLEEP_TIME Then
            $RIGHT_0 = 0; RESET RIGHT
         EndIf
      EndIf
   Endif
   
   Sleep(50)
WEND

Do you still need the SendKeyDelay since you can set your own seconds now?

Have fun :whistle:

EDIT: TYPO

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...