Sign in to follow this  
Followers 0
nf67

"Global" SendKeyDelay that lasts for the whole script?

15 posts in this topic

#1 ·  Posted (edited)

Greetings,

I was wondering if there is a command to alter the time in which a key is sent when that key is being held down :D ?

For example, let's say you hold the "a" key down for 1 sec and you get "aaaaa" which means that there is a key being sent every 0.2 sec. Is there a way to increase the 0.2?

I have HotKeys set to functions, but holding down that HotKey will repeat the Func, I want to change this so that if you press the HotKey once, the Func will occur once regardless of how long the HotKey is being held down.

Here's my an example script (Once again, you shouldn't be able to move the "ball" by holding down the hotkey, it should just move 5 pixels once.)

(Note: for some reason I couldn't get the hotkeys working with "{LCTRL}" and "{RCTRL}", so I now use q and p.)

(Update: this problem has been solved, here's the final code, fully documented.)

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

$BallCoord=195 ;Set the ball position to the middle of the GUI (horizontally).

$GUI = GUICreate("IaminCTRL", 410, 56)
GUISetBkColor(0x000000)
$RCTRLtext = GUICtrlCreateLabel("]R-CTRL", 300, 5, 105, 32)
GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
GUICtrlSetColor(-1, 0xFFFFFF)
$LCTRLtext = GUICtrlCreateLabel("L-CTRL[", 5, 5, 105, 32)
GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
GUICtrlSetColor(-1, 0xFFFFFF)
$BALL = GUICtrlCreateLabel("l", $BallCoord, 10, 20, 30)  ;Create the "ball", a webdings character, and use $BallCoord as its horizontal position.
GUICtrlSetFont(-1, 18, 800, 0, "Wingdings")
GUICtrlSetColor(-1, 0xFFFFFF)
$WinText = GUICtrlCreateLabel("", 105, 37, 202, 17)
GUICtrlSetColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)


While 1 ;Continiously...--------------
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ;...Check if the [X] is pressed
            Exit ;if so, exit.
    EndSwitch
    If _IsPressed("A2") then Lctrl() ;...Check if Left CTRL is pressed, if so, use function Lctrl()
    If _IsPressed("A3") then Rctrl() ;...Check if Right CTRL is pressed, if so, use function Rctrl()
WEnd ;--------------------------------

Func Lctrl() ;Create the function which is called by pressing A2(Left Control)

    $BallCoord=$BallCoord - 5 ;Substract 5 from the horizontal position of the ball.
    GUICtrlSetPos($BALL, $BallCoord, 10) ;Apply the new position.
        If $BallCoord < 115 Then ;Check if the new position is "below" (to the left of) 115 pixels, and if so...
            GUICtrlSetData($WinText, "L-CTRL WINS. PRESS * TO TRY AGAIN") ;Set the text at the bottom to L CTRL WINS. etc.
            HotKeySet("{NUMPADMULT}", "NewGame") ;Activate the HotKey * to start a new game by calling the NewGame() func.
            Do ;Disable further ball movement
            Sleep(20) ;By doing "nothing"
            Until _IsPressed("6A")  ;Until a new game is started with *
        Else
         While _IsPressed("A2") ;While the key is being held down
              sleep(20) ;Do "nothing", so you can't move the ball by simply holding down the key
         Wend

        EndIf
    EndFunc

Func Rctrl() ;Create the function which is called by pressing A3(Right Control)

    $BallCoord=$BallCoord + 5 ;Add 5 to the horizontal position of the ball.
    GUICtrlSetPos($BALL, $BallCoord, 10) ;Apply the new position.
        If $BallCoord > 275 Then ;Check if the new position is "over" (to the right of) 115 pixels, and if so...
            GUICtrlSetData($WinText, "R-CTRL WINS. PRESS * TO TRY AGAIN") ;Set the text at the bottom to R CTRL WINS. etc.
            HotKeySet("{NUMPADMULT}", "NewGame") ;Activate the HotKey * to start a new game by calling the NewGame() func.
            Do ;Disable further ball movement
            Sleep(20) ;By doing "nothing"
            Until _IsPressed("6A") ;Until a new game is started with *
        Else
         While _IsPressed("A3") ;While the key is being held down
              sleep(20) ;Do "nothing", so you can't move the ball by simply holding down the key
         Wend

        EndIf
    EndFunc

Func NewGame()
    $BallCoord=195 ;Reset the Ball position to the middle of the GUI
    GUICtrlSetPos($BALL, $BallCoord, 10) ;Apply the new position.
    GUICtrlSetData($WinText, "") ;Set the text at the bottom to ""(nothing)
    HotKeySet("{NUMPADMULT}") ;Deactivate the HotKey *
EndFunc

Thanks :D

Edited by nf67

Share this post


Link to post
Share on other sites



 

Opt("SendKeyDelay", 5)       ;5 milliseconds


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Maybe this will help you.

#Include <HotKey.au3>

Global Const $VK_P = 0x50
Global $i = 0

$Form = GUICreate('Test', 200, 200)
$Label = GUICtrlCreateLabel($i, 20, 72, 160, 52, 0x01)
GUICtrlSetFont(-1, 32, 400, 0, 'Tahoma')
GUISetState()

; Assign "P" with MyFunc() for created window only
_HotKeyAssign($VK_P, 'MyFunc', $HK_FLAG_NOREPEAT, $Form)

Do
Until GUIGetMsg() = -3
Func MyFunc()
    $i += 1
    GUICtrlSetData($Label, $i)
EndFunc   ;==>MyFunc

HotKey.au3

Edited by Yashied

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Opt("SendKeyDelay", 5)  ;5 milliseconds
Yeah that really seems like it should do the trick, but doesn't do anything :D , any special includes needed that I have missed?

Yashied, I am not advanced enough with AutoIt to understand your script, is your HotKey.au3 a UDF? Because just pasting it in my script (as if it were default) gives me an error. Thanks anyway :-)

Edited by nf67

Share this post


Link to post
Share on other sites

Apart from Yashied's solution in post #3, you could do something like this

Func Rctrl()
    HotKeySet("p")
    $BallCoord=$BallCoord + 5
    GUICtrlSetPos($BALL, $BallCoord, 10)
        If $BallCoord > 275 Then
            GUICtrlSetData($WinText, "R-CTRL WINS. PRESS * TO TRY AGAIN")
            HotKeySet("q");Replace with "{LCTRL}"
            HotKeySet("p");Replace with "{RCTRL}"
            HotKeySet("{NUMPADMULT}", "NewGame")
        Else
        ;wait for P to be released
         While _IsPressed("50")
              sleep(20)
         Wend
          HotKeySet("p","{RCTRL}")
        EndIf
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Yeah that really seems like it should do the trick, but doesn't do anything :D , any special includes needed that I have missed?

Oh, you know what, that corresponds with using the send command. I misunderstood what you were wanting. Sorry. That won't do what you want.


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Func Rctrl()
       HotKeySet("p")
       $BallCoord=$BallCoord + 5
       GUICtrlSetPos($BALL, $BallCoord, 10)
           If $BallCoord > 275 Then
               GUICtrlSetData($WinText, "R-CTRL WINS. PRESS * TO TRY AGAIN")
               HotKeySet("q");Replace with "{LCTRL}"
               HotKeySet("p");Replace with "{RCTRL}"
               HotKeySet("{NUMPADMULT}", "NewGame")
           Else
        ;wait for P to be released
            While _IsPressed("50")
                 sleep(20)
            Wend
             HotKeySet("p","{RCTRL}")
           EndIf
   EndFunc
Thanks, added #include <Misc.au3> and changed HotKeySet("p","{RCTRL}") to HotKeySet("p","Rctrl")

Do you by any chance also know how I can get the hotkeys to be set as the Ctrl buttons (instead of q and p)?

Especially since IsPressed ("11") doesn't have the difference between left and right :D .

>Updated the code in Post #1.

Edited by nf67

Share this post


Link to post
Share on other sites

Thanks, added #include <Misc.au3> and changed HotKeySet("p","{RCTRL}") to HotKeySet("p","Rctrl")

Do you by any chance also know how I can get the hotkeys to be set as the Ctrl buttons (instead of q and p)?

Especially since IsPressed ("11") doesn't have the difference between left and right :D .

>Updated the code in Post #1.

Apologies for the error.

You can detect the left and right control keys with _IsPressed("A2") or ("A3"), the problem is you can't set a hotkey for just the control key, so you would need to put _IsPressed in your main while loop instead of using hotkeyset.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Ok... I tried that and kind of broke my script :D

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

$BallCoord=195

$GUI = GUICreate("IaminCTRL", 410, 56)
GUISetBkColor(0x000000)
$RCTRLtext = GUICtrlCreateLabel("]R-CTRL", 300, 5, 105, 32)
GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
GUICtrlSetColor(-1, 0xFFFFFF)
$LCTRLtext = GUICtrlCreateLabel("L-CTRL[", 5, 5, 105, 32)
GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
GUICtrlSetColor(-1, 0xFFFFFF)
$BALL = GUICtrlCreateLabel("l", $BallCoord, 10, 20, 30)
GUICtrlSetFont(-1, 18, 800, 0, "Wingdings")
GUICtrlSetColor(-1, 0xFFFFFF)
$WinText = GUICtrlCreateLabel("", 105, 37, 202, 17)
GUICtrlSetColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)

HotKeySet("{NUMPADMULT}", "NewGame")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case _IsPressed("A2")
            Lctrl()
        Case _IsPressed("A3")
            Rctrl()
    EndSwitch
WEnd

Func Lctrl()
    HotKeySet("q")
    $BallCoord=$BallCoord - 5
    GUICtrlSetPos($BALL, $BallCoord, 10)
        If $BallCoord < 115 Then
            GUICtrlSetData($WinText, "L-CTRL WINS. PRESS * TO TRY AGAIN")
            HotKeySet("{NUMPADMULT}", "NewGame")
            Do                          ;Disable further ball movement
            Sleep(20)
            Until _IsPressed("6A")  ;Until a new game is started with *
        Else
         While _IsPressed("A2")
              sleep(20)
         Wend
          HotKeySet("A2","Lctrl") ;???
        EndIf
    EndFunc

Func Rctrl()
    HotKeySet("p")
    $BallCoord=$BallCoord + 5
    GUICtrlSetPos($BALL, $BallCoord, 10)
        If $BallCoord > 275 Then
            GUICtrlSetData($WinText, "R-CTRL WINS. PRESS * TO TRY AGAIN")
            HotKeySet("{NUMPADMULT}", "NewGame")
            Do                          ;Disable further ball movement
            Sleep(20)
            Until _IsPressed("6A") ;Until a new game is started with *
        Else
         While _IsPressed("A3")
              sleep(20)
         Wend
          HotKeySet("A3","Rctrl") ;???
        EndIf
    EndFunc

Func NewGame()
    $BallCoord=195
    GUICtrlSetPos($BALL, $BallCoord, 10)
    GUICtrlSetData($WinText, "")
    HotKeySet("{NUMPADMULT}")
EndFunc

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Something like this

#include <GUIConstantsEx.au3>
 #include <StaticConstants.au3>
 #include <WindowsConstants.au3>
 #Include <Misc.au3>
 
 $BallCoord=195
 
 $GUI = GUICreate("IaminCTRL", 410, 56)
 GUISetBkColor(0x000000)
 $RCTRLtext = GUICtrlCreateLabel("]R-CTRL", 300, 5, 105, 32)
 GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
 GUICtrlSetColor(-1, 0xFFFFFF)
 $LCTRLtext = GUICtrlCreateLabel("L-CTRL[", 5, 5, 105, 32)
 GUICtrlSetFont(-1, 18, 800, 0, "Arial Rounded MT Bold")
 GUICtrlSetColor(-1, 0xFFFFFF)
 $BALL = GUICtrlCreateLabel("l", $BallCoord, 10, 20, 30)
 GUICtrlSetFont(-1, 18, 800, 0, "Wingdings")
 GUICtrlSetColor(-1, 0xFFFFFF)
 $WinText = GUICtrlCreateLabel("", 105, 37, 202, 17)
 GUICtrlSetColor(-1, 0xFFFFFF)
 GUISetState(@SW_SHOW)
 
 HotKeySet("{NUMPADMULT}", "NewGame")
 [s]Global $LCtrlON = True, $RCtrlON = True[/s]
 While 1
     $nMsg = GUIGetMsg()
     Switch $nMsg
         Case $GUI_EVENT_CLOSE
             Exit
        
 EndSwitch
 
  If _IsPressed("A2") then  Lctrl()
  If _IsPressed("A3") then  Rctrl()
  
  WEnd
 
 Func Lctrl()
    
     $BallCoord=$BallCoord - 5
     GUICtrlSetPos($BALL, $BallCoord, 10)
         If $BallCoord < 115 Then
             GUICtrlSetData($WinText, "L-CTRL WINS. PRESS * TO TRY AGAIN")
             HotKeySet("{NUMPADMULT}", "NewGame")
            [s] $LCtrlON = False[/s]
             Do                     ;Disable further ball movement
             Sleep(20)
             Until _IsPressed("6A");Until a new game is started with *
         Else
          While _IsPressed("A2")
               sleep(20)
          Wend
           
         EndIf
     EndFunc
 
 Func Rctrl()
     
     $BallCoord=$BallCoord + 5
     GUICtrlSetPos($BALL, $BallCoord, 10)
         If $BallCoord > 275 Then
             [s]$RCtrlON = False[/s]
             GUICtrlSetData($WinText, "R-CTRL WINS. PRESS * TO TRY AGAIN")
             HotKeySet("{NUMPADMULT}", "NewGame")
             Do                     ;Disable further ball movement
             Sleep(20)
             Until _IsPressed("6A");Until a new game is started with *
         Else
          While _IsPressed("A3")
               sleep(20)
          Wend
           
         EndIf
     EndFunc
 
 Func NewGame()
     $BallCoord=195
     GUICtrlSetPos($BALL, $BallCoord, 10)
     GUICtrlSetData($WinText, "")
     HotKeySet("{NUMPADMULT}")
    [s] $LCtrlON = True
     $RCtrlON = True[/s]
 EndFunc

EDIT: Striked through the redundant $LCtrlON and $RCtrlON variables.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Ah, I have misplaced the checks in the loop :D ... could you please tell me a bit more about the

$LCtrlON = True and False? Where do you tell the script that it can't go on (until * is pressed ofcourse) when it's false? Or is this a built-in feature?

P.S: No SciTe on this PC so I can't test anything at the moment.

Edited by nf67

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Ah, I have misplaced the checks in the loop :D ... could you please tell me a bit more about the

$LCtrlON = True and False? Where do you tell the script that it can't go on (until * is pressed ofcourse) when it's false? Or is this a built-in feature?

P.S: No SciTe on this PC so I can't test anything at the moment.

Good question because the $LCtrlON and the $RCtrlON were for an idea which I didn't need. If you remove them everywhere the script will still work the same! I'll edit the script I posted.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Added the new code in the first post, fully documented.

Share this post


Link to post
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
Sign in to follow this  
Followers 0