Jump to content
Sign in to follow this  
chie

break out SLEEP issue

Recommended Posts

chie

Keywords: break sleep, stop sleep, exit sleep

Ecample:

Hire you see if you Hold down keyboard(PAUSE) it will exit loop, but only after 5 seconds, is it possible to exit loop immediately, without waiting the 5 seconds ?

while 1
    send("0")
    sleep(5000)
    send("0 ")
    sleep(5000)
If _IsPressed("13", $dll) Then ExitLoop
WEnd

Share this post


Link to post
Share on other sites
SmOke_N

You could use AdlibEnable() or /AutoIt3ExecuteScript or make your own custom UDF

#include <misc.au3>

While 1
    Send('0')
    If Not _Sleep_IsPressed(5000, '13') Then ExitLoop
    Send('0 ')
    If Not _Sleep_IsPressed(5000, '13') Then ExitLoop
WEnd

Func _Sleep_IsPressed($nSleep, $sIsPressed, $iDefault = 10)
    Local $iAdd, $nExitLoop = Int($nSleep) / Int($iDefault)
    Do
        Sleep($iDefault)
        $iAdd += 1
    Until $iAdd >= $nExitLoop Or _IsPressed($sIsPressed)
    If _IsPressed($sIsPressed) Then Return SetError(1, 0, 0)
    Return 1
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
SmOke_N

Could do it this way as well so you don't have to use _IsPressed() and the entire Misc.au3 file:

Global $nEscape = False
HotKeySet('{PAUSE}', '_ChangeValue')

While 1
    Send('0')
    If Not _SleepHK(5000) Then ExitLoop
    Send('0 ')
    If Not _SleepHK(5000) Then ExitLoop
WEnd

Func _SleepHK($nSleep, $iDefault = 10)
    Local $iAdd, $nExitLoop = Int($nSleep) / Int($iDefault)
    Do
        Sleep($iDefault)
        $iAdd += 1
    Until $iAdd >= $nExitLoop Or $nEscape
    If $nEscape Then
        $nEscape = Not $nEscape
        Return SetError(1, 0, 0)
    EndIf
    Return 1
EndFunc

Func _ChangeValue()
    $nEscape = Not $nEscape
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
chie

That seems to be really complicated. if it is not too mutch to ask, I would like to understand what you have done,so can you please edit your second post & add comments so others(less smarter people) would be able to learn from this, Please.

I tested both code pieses & first one was wery little bit less resource eating one but there was almost no conciderable differense, so I used this for my script:

Working Sample for others

#include <Misc.au3>
Hotkeyset("!1", "buttonhit")
IniWrite("C:\myfile.ini", "KeyHitSpeed", "speed", "5000")
Func buttonhit()
While 1
    $iniread_speed = IniRead("C:\myfile.ini", "KeyHitSpeed", "speed", "Not Found") ;check ini to get speed 
    If Not _Sleep_IsPressed($iniread_speed, '13') Then ExitLoop
    Send('0 ')
WEnd
EndFunc

;~ ---------------------------------------------------------------
Func _Sleep_IsPressed($nSleep, $sIsPressed, $iDefault = 10)
    Local $iAdd, $nExitLoop = Int($nSleep) / Int($iDefault)
    Do
        Sleep($iDefault)
        $iAdd += 1
    Until $iAdd >= $nExitLoop Or _IsPressed($sIsPressed)
    If _IsPressed($sIsPressed) Then Return SetError(1, 0, 0)
    Return 1
EndFunc

; Need to keep the program running until you press the hotkey
While 1
    Sleep(100)
WEnd

Share this post


Link to post
Share on other sites
SmOke_N

Sorry, I don't do comments too often. I figured it wasn't to difficult to follow what I was doing.

_Sleep_IsPressed(Sleep In Milliseconds you would normally do, The key you want to check in IsPressed, the default sleep time) ... We have to do a low sleep time in the Do/Until loop because we need to check if something was pressed, so the default is 10 milliseconds.

$iAdd is what we will add one too every loop to compare if we reach our $nSleep time.

$nExitLoop is the sent $nSleep divided by the default sleep for our Do/Until loop that allows us to properly use the $iAdd

If _Ispressed is true, then it returns 0 thus why we are using If Not _Sleep_IsPressed() rather than If _Sleep_IsPressed(), if _Ispressed wasn't used and the default sleep time ($nSleep) was reached, then the return is 1, allowing us to continue our condition (in this case your IniRead stuff).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Richard Robertson

Using Sleep with a value of 10 milliseconds is better. You don't have to worry about CPU usage because I've had tight loops using only minuscule sleep times without so much as a peep from my processor fan.

Share this post


Link to post
Share on other sites
SmOke_N

SmOke_N is too smart for his own good. :whistle:

;) Thanks (I think :P ) ... however, I hardly think the functions I provided shows an over abundance of intelligence.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
xcal

Well, you approach things with more of a udf mentality, which is smart (although can look more complicated). I just try to make things work for the given situation... which I kind of need to get away from. edit - Well, maybe not so much get away from, but expand from.

Edited by xcal

Share this post


Link to post
Share on other sites
AzKay

Well, you approach things with more of a udf mentality, which is smart (although can look more complicated). I just try to make things work for the given situation... which I kind of need to get away from. edit - Well, maybe not so much get away from, but expand from.

Because UDF's are usually easier to work with. In my opinion.

# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites
chie

Something unexpected happened, since all other shortcuts were taken already, i had to improvise but...im a little bit stuck again:

lets suppose I have chineese, russian,japaneese or any other keyboard that has those wierd signs on it, Does this means those signs or letters cant be used?

I tried to use a 2 key combination to exit loop CTRL+õ or CTRL+ö or CTRL+ä (Note I wanted to use as a second key a letter with a dots) but it seems it cant be used, so I wanted to make shure, if I can use it like this?:

_IsPressed("CTRL+õ", $dll) then ;
exit loop

Share this post


Link to post
Share on other sites
SmOke_N

To use 2 keys, you need 2 _IsPressed() functions... you also need to read on how to use _IsPressed() and what are legal characters to use.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
chie

To use 2 keys, you need 2 _IsPressed() functions... you also need to read on how to use _IsPressed() and what are legal characters to use.

1. yes works perfect, thank you
If _IsPressed("11", $dll) and _IsPressed("7B", $dll) Then exitloop ;CTRL+F12
2. & legal characters are only the ones that are listed under Function Reference,_IsPressed,Remarks:...... ? Edited by chie

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  

×