Jump to content
Sign in to follow this  
MuffettsMan

Sleep causing latency with _IsPressed

Recommended Posts

MuffettsMan

I have a script thats sending 0-9 keys to multiple windows based on ispressed, however the 'touchy' part of it seems to be determined by the sleep delay on the while loop... if set to short it sends multiple numbers, if set to high it seems to miss that the key was pressed at all... is there a more stable means of capturing and sending than being dependant on a perfect sleep timer? even if i found something that worked my concern would be when running from a different pc or even if the current is under heaver load.

my script works but hardly reliably.

#AutoIt3Wrapper_run_debug_mode=Y

#include <Misc.au3>

$dll = DllOpen("user32.dll")

Global $Notepad1[3][3]
Global $Notepad2[3][3]
Global $Notepad3[3][3]
Global $Mirrored

Global $NL

$NL = @CRLF

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 1" & @CRLF)
$Notepad1 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad1[1][0] & @LF & "Handle1=" & $Notepad1[1][1] & @CR)
LoopArray($Notepad1, "HWND: " & $Notepad1[1][1])

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 2" & @CRLF)
$Notepad2 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad2[1][0] & @LF & "Handle1=" & $Notepad2[1][1] & @CRLF & "Title2=" & $Notepad2[2][0] & @LF & "Handle2=" & $Notepad2[2][1]& @CRLF)
LoopArray($Notepad2, "HWND: " & $Notepad2[1][1])

Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is notepad 3" & @CRLF)
$Notepad3 = WinList ("Untitled - Notepad")
MsgBox(0, "Window: ", "Title1=" & $Notepad3[1][0] & @LF & "Handle1=" & $Notepad3[1][1] & @CRLF & "Title2=" & $Notepad3[2][0] & @LF & "Handle2=" & $Notepad3[2][1] & @CRLF & "Title3=" & $Notepad3[3][0] & @LF & "Handle3=" & $Notepad3[3][1])
LoopArray($Notepad3, "HWND: " & $Notepad3[1][1])

; func Broadcast to All
func LoopArray($window, $data)
    For $i = 1 to $window[0][0]
    ; MsgBox(0, "Window: ", "Trying to write to window " & $window[$i][0] & @LF & "Handle1=" & $window[$i][1] & @CRLF & "1: " & $Notepad1[1][1]  & @CRLF & "2: " & $Notepad2[1][1]  & @CRLF & "3: " & $Notepad3[1][1])
        ControlSend ($window[$i][1],"","Edit1","Data: " & $data & @CR)
    Next
EndFunc

func Broadcast()
    $data = InputBox("Data Input Request", @UserName & ", Please enter Data to spam all boxes...", "", "*")
    LoopArray($Notepad3, $data)
EndFunc

; Default Hot Keys:
HotKeySet("+!b", "Broadcast") ;Shift-Alt-b Broadcast to all Notepads
HotKeySet("+!1", "FirstPad") ;Shift-Alt-1
HotKeySet("+!2", "SecondPad") ;Shift-Alt-2
HotKeySet("+!3", "ThirdPad") ;Shift-Alt-3
HotKeySet("+!t", "ToggleMirror") ;Shift-Alt-t

Func FirstPad()
    ControlSend ($Notepad1[1][1],"","Edit1","Single Send: 1" & @CR)
EndFunc

Func SecondPad()
    ControlSend ($Notepad2[1][1],"","Edit1","Single Send: 2" & @CR)
EndFunc

Func ThirdPad()
    ControlSend ($Notepad3[1][1],"","Edit1","Single Send: 3" & @CR)
EndFunc

while 1
    sleep (500)
    If _IsPressed("30", $dll) Then; 0 Key
        ControlSend ($Notepad1[1][1],"","Edit1","0")
        ControlSend ($Notepad2[1][1],"","Edit1","0")
        ControlSend ($Notepad3[1][1],"","Edit1","0")
    EndIf
    If _IsPressed("31", $dll) Then; 1 Key
        ControlSend ($Notepad1[1][1],"","Edit1","1")
        ControlSend ($Notepad2[1][1],"","Edit1","1")
        ControlSend ($Notepad3[1][1],"","Edit1","1")
    EndIf
        If _IsPressed("32", $dll) Then; 2 Key
        ControlSend ($Notepad1[1][1],"","Edit1","2")
        ControlSend ($Notepad2[1][1],"","Edit1","2")
        ControlSend ($Notepad3[1][1],"","Edit1","2")
    EndIf
    If _IsPressed("33", $dll) Then; 3 Key
        ControlSend ($Notepad1[1][1],"","Edit1","3")
        ControlSend ($Notepad2[1][1],"","Edit1","3")
        ControlSend ($Notepad3[1][1],"","Edit1","3")
    EndIf
    If _IsPressed("34", $dll) Then; 4 Key 
        ControlSend ($Notepad1[1][1],"","Edit1","4")
        ControlSend ($Notepad2[1][1],"","Edit1","4")
        ControlSend ($Notepad3[1][1],"","Edit1","4")
    EndIf
    If _IsPressed("35", $dll) Then; 5 Key
        ControlSend ($Notepad1[1][1],"","Edit1","5")
        ControlSend ($Notepad2[1][1],"","Edit1","5")
        ControlSend ($Notepad3[1][1],"","Edit1","5")
    EndIf
    If _IsPressed("36", $dll) Then; 6 Key
        ControlSend ($Notepad1[1][1],"","Edit1","6")
        ControlSend ($Notepad2[1][1],"","Edit1","6")
        ControlSend ($Notepad3[1][1],"","Edit1","6")
    EndIf
    If _IsPressed("37", $dll) Then; 7 Key
        ControlSend ($Notepad1[1][1],"","Edit1","7")
        ControlSend ($Notepad2[1][1],"","Edit1","7")
        ControlSend ($Notepad3[1][1],"","Edit1","7")
    EndIf
    If _IsPressed("38", $dll) Then; 8 Key
        ControlSend ($Notepad1[1][1],"","Edit1","8")
        ControlSend ($Notepad2[1][1],"","Edit1","8")
        ControlSend ($Notepad3[1][1],"","Edit1","8")
    EndIf
    If _IsPressed("39", $dll) Then; 9 Key
        ControlSend ($Notepad1[1][1],"","Edit1","9")
        ControlSend ($Notepad2[1][1],"","Edit1","9")
        ControlSend ($Notepad3[1][1],"","Edit1","9")
    EndIf
    If _IsPressed("0D", $dll) Then; Enter Key
        ControlSend ($Notepad1[1][1],"","Edit1",@CRLF)
        ControlSend ($Notepad2[1][1],"","Edit1","{ENTER}")
        ControlSend ($Notepad3[1][1],"","Edit1",$NL)
    EndIf
WEnd

Func ToggleMirror()
    $Mirrored = NOT $Mirrored
    While $Mirrored
        sleep(100)
        ToolTip('Script is running in mirror mode:',0,0)
    WEnd
    ToolTip("")
EndFunc

Don't let that status fool you, I am no advanced memeber!

Share this post


Link to post
Share on other sites
DW1

change your sleep to something low like 10, and then change your _IsPressed to look like this:

If _IsPressed("30", $dll) Then; 0 Key
    While _IsPressed("30", $dll)
        Sleep(10)
    WEnd
    ControlSend($Notepad1[1][1], "", "Edit1", "0")
    ControlSend($Notepad2[1][1], "", "Edit1", "0")
    ControlSend($Notepad3[1][1], "", "Edit1", "0")
EndIf

That way there will be only one action for each press (because it will only trigger the action after the key is released, due to it being stuck in the while loop, while we are pressing the same keypress)

Share this post


Link to post
Share on other sites
DW1

BTW, change all of those _ispressed checks to reflect the info from my last post

Share this post


Link to post
Share on other sites
ChrisL

Also all those if then endifs are a bit inefficient

try doing this instead

Select
    Case  _IsPressed("30", $dll)
        While _IsPressed("30", $dll)
            Sleep(10)
        WEnd
        ControlSend($Notepad1[1][1], "", "Edit1", "0")
        ControlSend($Notepad2[1][1], "", "Edit1", "0")
        ControlSend($Notepad3[1][1], "", "Edit1", "0")

    Case _IsPressed("31", $dll)
        While _IsPressed("31", $dll)
            Sleep(10)
        WEnd
        ControlSend ($Notepad1[1][1],"","Edit1","1")
        ControlSend ($Notepad2[1][1],"","Edit1","1")
        ControlSend ($Notepad3[1][1],"","Edit1","1")

    Case Else
        sleep(10)
EndSelect

Share this post


Link to post
Share on other sites
MuffettsMan

awesome that makes it work rock solid and makes perfect sense now i just kick myself for not being able to think of that on my own

Thanks Again :P


Don't let that status fool you, I am no advanced memeber!

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  

×