Sign in to follow this  
Followers 0
VerteXslaPPy

Do an function only a specific time ?

10 posts in this topic

Hello all, 

 

for example i have a script :

 

#include <*.au3>


While 1
Adlibregister("messageBx",1)
WEnd


Func MessageBx()
if _isPressed($H)Then
Do this for 2secconds

ConsoleWrite("Hello !")

when the 2 secconds are over it should stop until i will press $H again


Endfunc

i saw something about a timer function but this didnt helped me alot because it was always spamming Hello ! again again again it didnt stoped, i want that the Function stop after the 2secconds even if i hold H, it should first restart the 2secconds if i release H and press it again

 

 

would be verry nice if some one can help me up.

Share this post


Link to post
Share on other sites



Adlibregster() should be called only one time and it will repeat on its own at the defined time.  Your going to be registering it over and over and over in a loop (with no delay at that)

That is just for starters.

Share this post


Link to post
Share on other sites

Delay will dont change anything of the speed from the script? also its impossible to do what i want?

Share this post


Link to post
Share on other sites

I am not sure what you want exactly.

You want a hotkey to launch a function.

Is that function supposed to only run one time in 2 seconds or over and over again for 2 seconds?

It may be better that you give us real life examples of what you want to do rather than theory.

Why do you need _IsPressed() ? do you want the function to stop if you let go of the key? 

Share this post


Link to post
Share on other sites

I am not sure what you want exactly.

You want a hotkey to launch a function.

Is that function supposed to only run one time in 2 seconds or over and over again for 2 seconds?

It may be better that you give us real life examples of what you want to do rather than theory.

Why do you need _IsPressed() ? do you want the function to stop if you let go of the key? 

 

no i use if pressed because :

 

 

when i press H it should write in the console for 3secconds this word Hello

 

 

so here what i do with my hands

 

 

press H (and hold it) it spams 3secconds Hello in the console,(im still holding H) it stop spamming(im still holding H......) now i release H, press H again (and hold it) it spams 3secconds Hello in the console,(im still holding H) it stop spamming(im still holding H......) now i release H, press H again 

 

 

sorry my english is verry bad but i hope you understand it now

Share this post


Link to post
Share on other sites

This is mostly working, the pres and hold works great but the on release is a big slow to take effect.  probably a much better way to do this but you can at least verify this is kind of what your trying to do and a better code writing can give you a proper solution. 

#Include <Misc.au3>

HotKeySet("H", "Loop")

While 1
    Sleep(10)
WEnd

Func Loop()
    HotKeySet("H")
    $vStart = TimerInit()
    While 1
    If _ISPressed("48", 'user32.dll') Then
        ConsoleWrite(@CRLF & "Function Running" & @CRLF & "$vStart = " & $vStart & @CRLF & "TimerDiff = " & TimerDiff($vStart) & @CRLF)
        If TimerDiff($vStart) > 200 Then ExitLoop
    Else
        ExitLoop
    EndIf
    WEnd

    While 1
            If _ISPressed("48", 'user32.dll') Then
                Sleep(10)
            Else
                ExitLoop
            EndIf
            WEnd
            HotKeySet("H", "Loop")
EndFunc

 

Share this post


Link to post
Share on other sites

thanks buddy this helped me a littlebit !

 

If TimerDiff($vStart) > 200 Then ExitLoop about this

 

ExitLoop is ending the While 1, is there something diferent expect ExitLoop to exit a function?

Share this post


Link to post
Share on other sites

ExitLoop exits the current loop, if its a Function you can use Return, and there is alwasy Exit to exit the entire script.

I used Exitloop in that first loop to take you into a second loop that stops the function if your still holding the hotkey.  Not until you release the hotkey is the second loop exited, and upon exit it reasigns the hotkey so that you can launch the function again. 

1 person likes this

Share this post


Link to post
Share on other sites

Yeah 2000 milliseconds, but the crazy code I came up with is not accurate and 200 was much closer to 2 seconds than 2000.

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