Jump to content
TheOnlyOne

Hotkeyset seems to stop working when i hit a while loop

Recommended Posts

TheOnlyOne

So currently I have some code waiting for a status to change(there is different indicators to check if this status has changed) 

Func oneRow($count)
    $globaltimer = _Timer_SetTimer($gui, 60000*10, "reset")
    $errTimer = TimerInit()
While (1)
        If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)
        Sleep(500)
        MouseClick("right")
        Sleep(600)
        If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)

        If TimerDiff($errTimer) > 5000 Then
            If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then
                Sleep(100)
                Send("{esc}")
                Sleep(1000)

            EndIf
        EndIf
    WEnd



....



EndFunc   ;==>oneRow

The two timers is for error checking. This works just fine the problem comes when I go into the while 1 loop, my hotkeys at the top of the program seems to stop working, for an example 

HotKeySet("{f1}", "stop")

Func stop()
    Exit
EndFunc   ;==>stop

Does not work at all after the while loop. The globaltimer is never called even though i tried setting the delay all the way down to 3 seconds. So I thought that maybe one of the function I use is blocking so the timer would never be called/the hotkey wouldn't work? if i make an delay just before the while loop the hotkey works just fine. Oh and the errTimer is supossed to not reset after 5 seconds but should keep making the check it does if five seconds have passed

Edited by TheOnlyOne
words are hard

Share this post


Link to post
Share on other sites
SadBunny

Can't reproduce, your code is not testable for anyone else, the rest of your code and/or the application that you're trying to work with and/or pretty much anything else may throw sand in the machine.

I tried to write a small reproducer that uses both types of timers at the same time as well and a hotkey, works fine for me:

#include <Timers.au3>

HotKeySet("x", "exiter")

doThing()
Exit

Func doThing()
    $globaltimer = _Timer_SetTimer(-1, 10000, "doBeep")
    $beepTimer = TimerInit()
    While (1)
        If TimerDiff($beepTimer) > 6000 and TimerDiff($beepTimer) < 8000 Then
            Beep(2000, 300)
        EndIf
    WEnd
EndFunc   ;==>doThing

Func doBeep()
    Beep(1000, 300)
    Sleep(100)
EndFunc   ;==>doBeep

Func exiter()
    Exit
EndFunc   ;==>exiter

Can you try and reproduce your issue with a standalone reproducer script?

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
AutoBert

Your script snipets should work, if the scipt is correctly completed, so post whole runable (reproducer) sdcript, showing your issue. But maybe another skript capures the F1-key?

Quote

If two AutoIt scripts set the same hotkeys, you should avoid running those scripts simultaneously as the second script cannot capture the hotkey unless the first script terminates or unregisters the key prior to the second script setting the hotkey.

 

Share this post


Link to post
Share on other sites
TheOnlyOne

Hmm not sure I can do that, I see that your example is working for me too. 

I have this code calling my function 

$return = 0
    For $i = 0 To 5
        If $pause = True Then
            pause()
        EndIf
        MsgBox(0,"","before one row")
        $return = oneRow($i)
        MsgBox(0,"",$return)
        If $return = 232 Then Return 232
        Sleep($waitDelay)
    Next

the msgboxes is something i inserted for debugging purposes. I also tried to add MsgBox(0,"","first") to the top of the oneRow function. So when i run the code it runs as expected the first time, shows "before one row" messagebox and afterwards the "first" msgbox. Then after 5 seconds it actually shows the  "before one row" msgbox again so it must have passed one of my pixelgetcolor calls(even though the status isnt correct yet). So i tried adding this to the if block that passes

If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then
                MsgBox(0,"","sucess " & @crlf & hex(PixelGetColor(1100, 310)) & @crlf & hex(PixelGetColor(901, 305)) & @crlf & hex(PixelGetColor(1043, 383)))
                Sleep(100)
                Send("{esc}")
                Sleep(1000)

            EndIf

But the msgbox is never shown. If i add an empty msgbox it is shown. I tried without the hex call but with the same result, is the pixegetcolor function returning errors maybe, i have never seen that before?

Another weird thing is after it shows the "before one row" msgbox the second time it doesnt show the next msgbox at the top of oneRow function ? As you said my code is properly to messy atm. I would have a hard time making a reproducable example which is not very good..

Edited by TheOnlyOne
removed duplicate code tags

Share this post


Link to post
Share on other sites
SadBunny

I'm not saying whether or not your code is messy, and even messy code should still work if it is actually correct (just harder to work with :)).

If you want to know how far your code gets during execution, there are a few shortcuts you could take to get some information, such as enabling the TrayIconDebug option or using Tools -> Trace: add trace lines in Scite. You can always add your own ConsoleWrite commands to show more debug information, such as results, errors and extended error information returned from function calls. (Much more useful than MsgBox'es IMHO as they don't require interaction, don't overlap any gui etc..)

I personally cannot see anything intrinsically wrong with this code (maybe someone else can?), but without clear information on when what happens exactly, it's blindfolded troubleshooting, at least to me. But that's what reproducers are for. The thing with reproducers is that if you feel like you would have a hard time making one, it's probably a sign that you have to rethink your solution to begin with, otherwise it's just trial-and-error to find a working band-aid...

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
water

I'm not sure but I think "Sleep" is a blocking function.
The help file tells us: "If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the blocking function completes."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
SadBunny

HotKeySet functionality remains active and also works immediately during a sleep.

/edit Tested with:

HotKeySet("x", "quitter")

Beep(1000, 200)
sleep(20000)

Func quitter()
    Beep(1500, 200)
    Exit
EndFunc   ;==>quitter

 

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
TheOnlyOne

Yea I might have to rethink the solution.. just annoys me because I feel like it should work. I just tried to add a tooltip to the while loop in oneRow() with the timerdiff inside. So the hotkey apperently works until 30-40 seconds have passed ?? there is no other code being executed other than the code inside the loop ..

Edited by TheOnlyOne

Share this post


Link to post
Share on other sites
Jos

Show me a simple reproducer of your script which actually works and we can check as to why you are seeing this.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
TheOnlyOne

Okay I might have found the problem with why the hotkeyset didn't work. I took another look at your first example @SadBunny and saw you used -1 for the hwnd. I thought you had to give it a GUI reference so i had created one like this, with the timer referencing it as shown earlier:

$gui = GUICreate("", 0, 0)

$globaltimer = _Timer_SetTimer($gui, Random(6000 * 2, 6000 * 5, 1), "reset")

I tried changing $gui to -1 and now the hotkey works even after 60+ seconds why is that? The function reset does not seem to be called though ? Tried chaning it to 10 seconds instead of a random generated number but with the same result?

Func reset()
    _Timer_KillTimer($gui, $globaltimer)
    MsgBox(0, "", "in reset")
    ...
EndFunc   ;==>reset

the callback function never seems to be called in your example nether

Edited by TheOnlyOne

Share this post


Link to post
Share on other sites
SadBunny

True, and I didn't want/need it to, I just wanted it to exist. Now that you mention it, the result of that _Timer_SetTimer returns 0 so the timer call fails and my test is faulty. That means that in your new case, the timer probably isn't created either and now your hotkey now remains working. So the problem still looks to be with that combination, or some contingency combined with that combination.

Finding out which one of those two is the case is a matter of elimination :) Either start from scratch with that combination and see if you can reproduce, or snip away every other contingency until you arrive at the last contingency that determines whether this problem occurs or not, and you can write a reproducer (and/or find your error :) ).

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
SadBunny

So here the timer actually works :) Maybe this is better to play around with?

#include <Timers.au3>

HotKeySet("x", "exiter")

Global $gui = GUICreate("hello", 300, 300, 100, 100)
;~ GUISetState(@SW_SHOW, $gui)

doThing()
Exit

Func doThing()
    Local $globaltimer = _Timer_SetTimer($gui, 6000, "doTimerFunction")
    ConsoleWrite("Timer id: " & $globaltimer & @CRLF)
    $beepTimer = TimerInit()
    While (1)
        If TimerDiff($beepTimer) > 3000 And TimerDiff($beepTimer) < 4000 Then
            Beep(1500, 100)
        EndIf
    WEnd
    Sleep(10000)
EndFunc   ;==>doThing

Func doTimerFunction($hWnd, $iMsg, $iIDtimer, $iTime)
    Beep(1000, 300)
EndFunc   ;==>doBeep

Func exiter()
    Exit
EndFunc   ;==>exiter

 

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
TheOnlyOne

Oh god i feel so stupid ..  my reset function didn't have any parameters.. Well thanks for your help, would properly been stuck with this for atleast a few hours more without it. 

Share this post


Link to post
Share on other sites
SadBunny

See, this is why reproducers are so important :) 9 out of 10 times (if not more) even the process of trying to come up with a reproducer from scratch, or stripping your code down until the problem disappears and then looking at the last thing you stripped, shows the actual problem.

Well, glad I could help :cheer:

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
SadBunny

P.S. Small note: where you write "properly" (also earlier in this thread) you meant "probably". I'm not a grammar Nazi or anything, but confusing those two can lead to actual confusion :)

  • Like 1

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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

  • Similar Content

    • guinness
      By guinness
      Right now the script will exit immediately if I don't pause it somehow (obvious of course).
      I was  curious on people's opinions of how they do it or what option they prefer below? Thanks
      Option 1 (I prefer)
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) ProcessWaitClose(@AutoItPID) Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc Option 2 (in the help file)
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) While Sleep(100) WEnd Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc Option 3
      #include <MsgBoxConstants.au3> HotKeySet('^!p', SomeFunc) While 1 GUIGetMsg() ; Use the internal sleep WEnd Func SomeFunc() MsgBox($MB_SYSTEMMODAL, "Title", "This message box will timeout after 10 seconds or select the OK button.", 10) EndFunc  
    • guner7
      By guner7
      Hi everyone,
      I would like to share a simple snippet where I use for my hotkey script.
      I was searching for a solution whereby pressing a same key, I could have different outcome according to the way the key is being pressed.
      1) Double tap/press fast (like double click the mouse) - outcome A
      2) Long press or HOLD for awhile (2-4 sec) - outcome B
      #Include <Timers.au3> Global $interval_timer, $counter HotKeySet("{F10}", "_terminate") While 1 Sleep(10) WEnd Func _terminate() Local $itv_timer $itv_timer = _Timer_Diff($interval_timer) $interval_timer = _Timer_Init() ;ConsoleWrite($itv_timer & @CRLF) ;to review the timer difference so I can decide the range to capture double tap sequence If $itv_timer > 100 And $itv_timer < 250 Then $counter = 0 $interval_timer = _Timer_Init() MsgBox("","","double tap detected") Elseif $counter >= 25 Then $counter = 0 $interval_timer = _Timer_Init() MsgBox("","","long press detected") ElseIf $itv_timer < 50 Then $counter = $counter + 1 ConsoleWrite($counter & @CRLF) Else $counter = 0 EndIf EndFunc The trick here is to collect and use the frequency of the hotkey is called, and the time difference between each time the hotkey is being called.
      My coding style is not efficient and can be such a mess most of the time, any suggestion or better coding are welcome.
      I hope you find this one helpful.
    • MrCheese
      By MrCheese
      Hi guys,
       
      Wondering, is there a better way, likely to use 'for...next' to add a letter to each range, by moving right -> along a range of columns in excel.
      I currently use this, but its clunky.
      If $run = 1 Then $range = "B6:B41" If $run = 2 Then $range = "C6:C41" If $run = 3 Then $range = "D6:D41" If $run = 4 Then $range = "E6:E41" If $run = 5 Then $range = "F6:F41" If $run = 6 Then $range = "G6:G41" If $run = 7 Then $range = "H6:H41" If $run = 8 Then $range = "I6:I41" If $run = 9 Then $range = "J6:J41" If $run = 10 Then $range = "K6:K41" If $run = 11 Then $range = "L6:L41" If $run = 12 Then $range = "M6:M41" If $run = 13 Then $range = "N6:N41" If $run = 14 Then $range = "O6:O41" If $run = 15 Then $range = "P6:P41" If $run = 16 Then $range = "Q6:Q41" If $run = 17 Then $range = "R6:R41" If $run = 18 Then $range = "S6:S41" If $run = 19 Then $range = "T6:T41" If $run = 20 Then $range = "U6:U41" If $run = 21 Then $range = "V6:V41" If $run = 22 Then $range = "W6:W41" If $run = 23 Then $range = "X6:X41" If $run = 24 Then $range = "Y6:Y41" If $run = 25 Then $range = "Z6:Z41" If $run = 26 Then $range = "AA6:AA41" If $run = 27 Then $range = "AB6:AB41" If $run = 28 Then $range = "AC6:AC41" If $run = 29 Then $range = "AD6:AD41" If $run = 30 Then $range = "AE6:AE41" If $run = 31 Then $range = "AF6:AF41" If $run = 32 Then $range = "AG6:AG41" If $run = 33 Then $range = "AH6:AH41" If $run = 34 Then $range = "AI6:AI41" If $run = 35 Then $range = "AJ6:AJ41" If $run = 36 Then $range = "AK6:AK41" If $run = 37 Then $range = "AL6:AL41" If $run = 38 Then $range = "AM6:AM41" If $run = 39 Then $range = "AN6:AN41" If $run = 40 Then $range = "AO6:AO41" If $run = 41 Then $range = "AP6:AP41" If $run = 42 Then $range = "AQ6:AQ41" If $run = 43 Then $range = "AR6:AR41" If $run = 44 Then $range = "AS6:AS41" If $run = 45 Then $range = "AT6:AT41" If $run = 46 Then $range = "AU6:AU41" If $run = 47 Then $range = "AV6:AV41" If $run = 48 Then $range = "AW6:AW41" If $run = 49 Then $range = "AX6:AX41" If $run = 50 Then $range = "AY6:AY41"  
      Normally, if it was going down the rows, i'd use this:
       
      For $i = 0 To UBound($iRowCount) - 1 $row = $i + 1 $range = "B"&$row&":B"&$row+1 Next  
      so something like this, but i don't know how to code sequential columns:
      For $i = 0 To UBound($iColCount) - 1 $col = $i + 1 $range = $col&"1:"&$col&"40" Next  
      If I don't make sense, let me know.
      Any help would be great. thanks
    • Rskm
      By Rskm
      Hi, I wrote the below code to learn user defined functions.. jam is the function which calculates summation and division of two numbers $a, $b.. I expect the $sum and $div receives the results $r and $s respectively from the function... but the msbox in the main program doesn't display results properly, while the one inside func shows it correclty. how do I pull the values in $r and $s to $sum and $div respectively.. thanks for all the help on this forum
       
      global $a,$b,$sum,$div
      $a=1
      $b=2 jam($a,$b,$sum,$div) msgbox(0,"results",$sum&" , "&$div)     func jam($p,$q,$r,$s )
       $r=$p+$q  $s = $p/$q msgbox(0,"results",$r&" , "&$s) EndFunc  
    • KING_NK
      By KING_NK
      #RequireAdmin
      Global $x=0
      Global $y=0
      Func Start()
      While 1
      AutoItSetOption('MouseCoordMode',0)
      AutoItSetOption('SendKeyDelay',10)
      WinActivate('XXX')
      MouseClick('Primary',852,62,1,10)
      sleep(2200)
      MouseClick('Primary',880,196,1,10)
      Send('260',0)      -----------------------> $a as 260
      MouseClick('Primary',880,196,1,0)
      sleep(2200)
      MouseClick('Primary',1019,194,1,10)
      Sleep(2200)
      Send('98',0) ------------------------->$b as 98
      sleep(1000)
      Wend
      EndFunc
       
       
      I want to use Different number in every loop in the place of $a and  $b.
      Is it possble?If it possible Kindly help me
×