Sign in to follow this  
Followers 0
repruw

Making a timer that would read from user input

20 posts in this topic

Hello, I have been playing around with AutoIT for a few hours now trying to make a simple program with start/stop button that will loop the code. I also want to add two input boxes for it -- one for which key would be pressed and the other one for the delay.

Key1 = key to be pressed

Delay1 = delay before it is pressed again

So when the user would press start the program would start looping the code that would be in the while field, but when the Delay1 time would be greater than the one the user wrote in the Delay1 field, a specific IF statement would be launched that would press Key1 and reset the Delay1 timer back to 0. When the Delay1 timer would be greater than Delay1 again, the IF statement would be launched again. All of this would loop until the person presses the Stop button or exits it.

I hope someone could write me an example of this so I can continue to build/learn on it. I hope all of this made sense.

Thanks in advance and sorry for my English.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Yes all this can be done, i suggest that you can use the only one loop to do this and do some filter with "If" statements, like you said, but first i suggest that start writing the code for the controls and do the main loop to keep the window open and check when to exit.

What is your experience in drawing controls into a GUI?

EDITED: I almos forgot it... Welcome to the forum. :)

Edited by monoscout999

Share this post


Link to post
Share on other sites

Welcome!

So you want:

Key press

Wait for delay to be reached

Reset and repeat ?

If yes, then look at send, sleep and maybe while loops.

But as Monoscout sais, drawing the gui is the key element so you should start there!


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

theres also the TimerInt()(not sure how to spell the function, you can find it in the help file looking up Timer) and TimerDiff() functions to look at.

EDIT: Welcom to the forum!

Edited by pieeater

[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites

I looked at _Timer_Init and _Timer_Diff and imo it is a bit confusing for a beguinner.

So i would suggest Sleep($Delay)


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

Doesn't sleep pause the whole program? Can someone read my original post and tell me how I would make a TimerDiff work with custom input?

Share this post


Link to post
Share on other sites

use help file

$input=guictrlcreateinput("", x, y, x1, y1)

$read=guictrlread($input)

$time=$read

while 1

$time=$time-1

wend

any way i think this is what you want

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

@repruw

yes sleep() pauses the whole program

@HeavenlyDemon

that code would not work. take a look at some guictrlcreate examples in the help file

Edited by pieeater

[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Doesn't sleep pause the whole program?

Isnt that what you want ? Edited by Maffe811

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

oh you want to count up and once the count is higer you want to send a defined key. use a for...to...next loop.

like this:

for $i = 1 To [variable containing the maximum delay]
     sleep(1000)
next
;rest of script
Edited by pieeater

[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
Share on other sites

I think he wants:

While 1
Send("a")
Sleep(1000)
Wend

But he wants the letter and time to be set in a gui before code execution.


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Like this ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


GUICreate("",200,100)
$ButtonOk = GUICtrlCreateButton("OK",70,65,60, 30)
$KeyInput = GUICtrlCreateInput("Key",10,10,180,20)
$DelayInput = GUICtrlCreateInput("Delay in seconds",10,40,180,20)

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
Select
    Case $Msg = $ButtonOk
        $Key = GUICtrlRead($KeyInput)
        $Delay = GUICtrlRead($DelayInput)
        If $Delay <> "Delay in seconds" and $Key <> "Key" then
        $Delay = $Delay * 1000
        MsgBox("","",$Key & " " & $Delay)
        KeyPresser()
        EndIf
    Case $Msg = $GUI_EVENT_CLOSE
        Exit
EndSelect

Sleep(40)
Wend

Func KeyPresser()
    While 1
        Send($Key)
        Sleep($Delay)
    WEnd
EndFunc

Edit:

Oh buger... I didnt read the loop code part -.-

Ignore the code above...

Edited by Maffe811

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Like this ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


GUICreate("",200,100)
$ButtonOk = GUICtrlCreateButton("OK",70,65,60, 30)
$KeyInput = GUICtrlCreateInput("Key",10,10,180,20)
$DelayInput = GUICtrlCreateInput("Delay in seconds",10,40,180,20)

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
Select
    Case $Msg = $ButtonOk
        $Key = GUICtrlRead($KeyInput)
        $Delay = GUICtrlRead($DelayInput)
        If $Delay <> "Delay in seconds" and $Key <> "Key" then
        $Delay = $Delay * 1000
        MsgBox("","",$Key & " " & $Delay)
        KeyPresser()
        EndIf
    Case $Msg = $GUI_EVENT_CLOSE
        Exit
EndSelect

Sleep(40)
Wend

Func KeyPresser()
    While 1
        Send($Key)
        Sleep($Delay)
    WEnd
EndFunc

Edit:

Oh buger... I didnt read the loop code part -.-

Ignore the code above...

Yes something like that only that there would be a While loop when the OK button is pressed and inside that While there would be IF statement for when the timer would be > $Delay.

While 1
    $msg = GUIGetMsg()
    
    Select
        
        Case $msg = $startbutton
                
                $send1 = GUICtrlRead($key)
                $sleep1 = GUICtrlRead($input)
                
            While 1 
                Send("{1}")
                Sleep(1000)
                
            ??????  If ??????? > ????? Then ??????
                    Send($send1)
                EndIf
            WEnd
            
    EndSelect
        
WEnd

I want to know how to make a timer at the ????? part of the code so that it would execute it only once when the timer is greater than $sleep1 and then continue to loop While.

Edited by repruw

Share this post


Link to post
Share on other sites

Look for TimerInit(), TimerDiff() in the help-file, that is what your code is missing.

Share this post


Link to post
Share on other sites

I did try adding that but it doesn't work for some reason. I don't know how to make it so it resets the time to 0 after it executes the IF function but still use the inputted time for the next time it goes over the inputted number.

Share this post


Link to post
Share on other sites

To reset time just set the timer variable to TimerInit() again, post the full code you attempted using timers.

Share this post


Link to post
Share on other sites

Here is a quick example of using timers, try to incorporate it into your script. If you get stuck, post your attempt.

$iTimeInSeconds = 5
$iTimeStamp = TimerInit()
While 1
    If TimerDiff($iTimeStamp) > $iTimeInSeconds * 1000 Then
        MsgBox(64, @ScriptName, "This message box will show every " & $iTimeInSeconds & " seconds.")
        $iTimeStamp = TimerInit()
    EndIf
WEnd

:mellow:

Share this post


Link to post
Share on other sites

To reset time just set the timer variable to TimerInit() again, post the full code you attempted using timers.

GUICreate("test", 300, 100)
GUISetState(@SW_SHOW)

$key = GUICtrlCreateInput("", 50, 15, 100)
$input = GUICtrlCreateInput("", 50, 40, 100)
$startbutton = GUICtrlCreateButton("start", 150, 15, 100)

While 1
    $msg = GUIGetMsg()
    
    Select
        
        Case $msg = $startbutton
                
                $send1 = GUICtrlRead($key)
                $sleep1 = GUICtrlRead($input) * 1000
                
            While 1 
                Send("{1}")
                Sleep(1000)
                
                If TimerDiff($sleep1) > $sleep1 Then
                    Send($send1)
                    $sleep1 = TimerInit()
                EndIf
            WEnd
        
    EndSelect
        
WEnd

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

i see two problems without run your scirpt

1 - You have to do the TimerDiff() before using TimerInit()

I think that the place to put TimerInit() must be before the second loop starts.

2 - you are overwriting $sleep1

TimerInit return a timestamp into the variable $sleep1 and $sleep1 have the value to check, so use another variable fot the timers.

EDITED: some spelled corrections

Edited by monoscout999

Share this post


Link to post
Share on other sites

i see two problems without run your scirpt

1 - You have to do the TimerInit() before using TimerDiff()

I think that the place to put TimerInit() must be before the second loop starts.

2 - you are overwriting $sleep1

TimerInit return a timestamp into the variable $sleep1 and $sleep1 have the value to check, so use another variable fot the timers.

Got it to work. Thanks to everyone.

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