Jump to content

Do an function only a specific time ?


Recommended Posts

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.

Link to comment
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? 

Link to comment
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

Link to comment
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

 

Link to comment
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. 

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...