AlwaysLearning Posted April 9, 2015 Share Posted April 9, 2015 (edited) Hello, I'm trying to make a script that checks for a UI element to tell if the correct mode is toggled turned on. The native program's UI displays the status of this mode via a thumbnail. I'm trying to get the script to toggle the mode between true or false, but sometimes it gets messed up when I'm pressing the keys very quickly or switching between functions (calling a function more than once, Send command doesn't send, etc). So I tried to add a self correct function that would scan the thumbnails at the top of the program menu. This would update and self-correct those errors every two seconds or so (in reality I want to do it faster, but two seconds seemed like a safe "test run"). It's simply not working the way I intended, and I've tried so many different approaches. Here is the latest approach: expandcollapse popupGlobal $hkt = TimerInit() Global $hktd Global $overide Global $cmarr[10] Global $cmpressable Global $cmmode HotKeySet ("{1}", "HotKey1") HotKeySet ("{2}", "HotKey2") HotKeySet ("{3}", "HotKey3") HotKeySet ("{4}", "HotKey4") HotKeySet ("{5}", "HotKey5") HotKeySet ("{6}", "HotKey6") HotKeySet ("{7}", "HotKey7") HotKeySet ("{8}", "HotKey8") HotKeySet ("{9}", "HotKey9") HotKeySet ("{0}", "HotKey0") HotKeySet ("{-}", "HotKey11") HotKeySet ("{=}", "HotKey12") While 1 = 1 $hktd = TimerDiff($hkt) ;; Yes, I know this is processor intensive. In reality there are some other instructions in the While loop, but for now I'll add a Sleep ( 50 ) at the bottom of loop. If $hktd >= 2000 Then ;; I added these time delays to prevent it from trying to correct the toggle status too often (the program UI takes some time to display which mode it is in). $override = False For $cm = 0 to UBound($cmarr) - 1 $hktd = TimerDiff($hkt) $cmpressable = PixelGetColor(1290, 876) $xposition = 600 + ($cm * 25) $cmarr[$cm] = PixelGetColor($xposition, 55) If $hktd >= 2000 and $cmarr[$cm] = 15636838 and $override = False Then ;; I added these time delays to prevent it from trying to correct the toggle status too often (the program UI takes some time to display which mode it is in). $cmmode = True ;; This seems to cause some trouble when the For...Next loop is sometimes interrupted via HotKeyX() ExitLoop ElseIf $hktd >= 2000 and $override = false and $cm = UBound($cmarr) - 1 Then $cmmode = False ;; This seems to cause some trouble when the For...Next loop is sometimes interrupted via HotKeyX() EndIf Next ;; I also tried doing an _ArraySearch function here after the loop completes, but it's still the same problem. The UI element appeared at $cm[3] while the For... Next loop was searching on $cm[4]+, the For...Next therefore thinks "NOT FOUND, LETS DECLARE IT FALSE", but in reality it showed up with bad timing. EndIf Sleep ( 50 ) ;; Not actually in my script. Just added for this forum post. WEnd Func HotKey1() HotKeySet ("{1}") $hktd = TimerDiff($hkt) If $cmmode = True Then $cmpressable = PixelGetColor(1290, 876) If $cmpressable = 9013641 Then $override = True $cmmode = False Send ("9") $hkt = TimerInit() EndIf EndIf Sleep (250) ;; Just put these sleeps in a few of the calls to see if it improved the functions, it didn't seem to make a difference. Send ("{1}") HotKeySet ("{1}", "HotKey1") EndFunc Func HotKey2() HotKeySet ("{2}") $hktd = TimerDiff($hkt) If $cmmode = True Then $cmpressable = PixelGetColor(1290, 876) If $cmpressable = 9013641 Then $hkt = TimerInit() Send ("9") $override = True $cmmode = False EndIf EndIf Sleep (250) Send ("2") HotKeySet ("{2}", "HotKey2") EndFunc Func HotKey3() HotKeySet ("{3}") $hktd = TimerDiff($hkt) If $cmmode = True Then $cmpressable = PixelGetColor(1290, 876) If $cmpressable = 9013641 Then $hkt = TimerInit() $override = True $cmmode = False Send ("9") EndIf EndIf Sleep (250) Send ("3") HotKeySet ("{3}", "HotKey3") EndFunc Func HotKey4() HotKeySet ("{4}") If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("4") EndIf Sleep (250) Send ("4") HotKeySet ("{4}", "HotKey4") EndFunc Func HotKey5() HotKeySet ("{5}") If $cmmode = True Then $override = True $cmmode = False Send ("9") $hkt = TimerInit() Else Send ("5") EndIf Sleep (250) Send ("5") HotKeySet ("{5}", "HotKey5") EndFunc Func HotKey6() HotKeySet ("{6}") If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("6") MouseMove (840, 435, 0) EndIf Sleep (250) HotKeySet ("{6}", "HotKey6") EndFunc Func HotKey7() HotKeySet ("{7}") $hktd = TimerDiff($hkt) If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("7") EndIf Sleep (250) HotKeySet ("{7}", "HotKey7") EndFunc Func HotKey8() HotKeySet ("{8}") $hktd = TimerDiff($hkt) If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("8") EndIf Sleep (250) HotKeySet ("{8}", "HotKey8") EndFunc Func HotKey9() HotKeySet ("{9}") Send ("{9}") HotKeySet ("{9}", "HotKey9") EndFunc Func HotKey0() HotKeySet ("{0}") Send ("{0}") HotKeySet ("{0}", "HotKey0") EndFunc Func HotKey11() HotKeySet ("{-}") Send ("{-}") HotKeySet ("{-}", "HotKey11") EndFunc Func HotKey12() HotKeySet ("{=}") Send ("{=}") HotKeySet ("{=}", "HotKey12") EndFunc Edited April 9, 2015 by AlwaysLearning Link to comment Share on other sites More sharing options...
AlwaysLearning Posted April 10, 2015 Author Share Posted April 10, 2015 I actually managed to fix it while cleaning up my code in the above post. I needed to make sure that it only declares False at the END of the array For...Next loop. Link to comment Share on other sites More sharing options...
argumentum Posted April 10, 2015 Share Posted April 10, 2015 I actually managed to fix it while cleaning up my code in the above post. I needed to make sure that it only declares False at the END of the array For...Next loop. so then post your fixed code and mark the topic as answered computergroove 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Solution AlwaysLearning Posted April 10, 2015 Author Solution Share Posted April 10, 2015 (edited) so then post your fixed code and mark the topic as answered That would be a good idea. I'm totally new here and didn't know the etiquette. I'll be more careful next time :-) Here is the fixed code: expandcollapse popupGlobal $cmarr[10] Global $cmmode = False Global $hkt = TimerInit() Global $hktd = TimerInit() Global $override Global $cmpressable ;; Many of these variables don't need to be Global, but I set it this way to help simplify debugging. HotKeySet ("{1}", "HotKey1") HotKeySet ("{2}", "HotKey2") HotKeySet ("{3}", "HotKey3") HotKeySet ("{4}", "HotKey4") HotKeySet ("{5}", "HotKey5") HotKeySet ("{6}", "HotKey6") HotKeySet ("{7}", "HotKey7") HotKeySet ("{8}", "HotKey8") HotKeySet ("{9}", "HotKey9") HotKeySet ("{0}", "HotKey0") HotKeySet ("{-}", "HotKey11") HotKeySet ("{=}", "HotKey12") While 1 = 1 $hktd = TimerDiff($hkt) ;; Check how much time has passed since the last time the mode was toggled (prevents this script from running before the native applications UI has a chance to display the thumbnail on the screen) If $hktd >= 500 Then $override = False ;; It is now OK for this portion of the script to look at the thumbnail bar and change the mode if necessary. This is here because sometimes the HotKey function will interrupt the For...Next loop below. If that happens, I need the script to know not to make any changes on its own after it goes back from finishing the HotKey function continues where it left off in its looping process. For $cm = 0 to UBound($cmarr) - 1 $cmpressable = PixelGetColor(1290, 876) ;; Check if mode can be changed (Hotkeys 1-3 have a 5 second loading window on occassion, so this variable is necessary to make sure the current task has finished) $xposition = 600 + ($cm * 25) ;; This is for scanning the thumbnail bar. $cmarr[$cm] = PixelGetColor($xposition, 55) ;; check the pixel at location If $cmarr[$cm] = 15636838 And $override = False Then $cmmode = True ExitLoop ElseIf $override = False and $cm = UBound($cmarr) - 1 Then ;; THIS was the problem. I needed to make sure this line only runs on the LAST round going through this loop. Otherwise, my results would be somehting like "False, False, True, False, False, False". The True in the middle was overwritten by a "False" result in the subsequent loops) $cmmode = False EndIf Next EndIf WEnd Func HotKey1() HotKeySet ("{1}") $hktd = TimerDiff($hkt) $cmpressable = PixelGetColor(1290, 876) ;; buttons 1-3 need to check if the option to toggled is available or not If $cmmode = True and $cmpressable = 9013641 Then $hkt = TimerInit() $override = True $cmmode = False Send ("9") Else Send ("{1}") EndIf HotKeySet ("{1}", "HotKey1") EndFunc Func HotKey2() HotKeySet ("{2}") $hktd = TimerDiff($hkt) $cmpressable = PixelGetColor(1290, 876) ;; buttons 1-3 need to check if the option to toggled is available or not If $cmmode = True and $cmpressable = 9013641 Then $hkt = TimerInit() $override = True $cmmode = False Send ("9") Else Send ("2") EndIf HotKeySet ("{2}", "HotKey2") EndFunc Func HotKey3() HotKeySet ("{3}") $hktd = TimerDiff($hkt) $cmpressable = PixelGetColor(1290, 876) ;; buttons 1-3 need to check if the option to toggled is available or not If $cmmode = True and $cmpressable = 9013641 Then $hkt = TimerInit() $override = True $cmmode = False Send ("9") Else Send ("3") EndIf HotKeySet ("{3}", "HotKey3") EndFunc Func HotKey4() HotKeySet ("{4}") If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("4") EndIf HotKeySet ("{4}", "HotKey4") EndFunc Func HotKey5() HotKeySet ("{5}") ;If $cmmode = False Then ; $override = True ; $cmmode = True ; Send ("9") ; $hkt = TimerInit() ;Else ; Send ("5") ;EndIf Send ("5") HotKeySet ("{5}", "HotKey5") EndFunc Func HotKey6() HotKeySet ("{6}") If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("6") EndIf HotKeySet ("{6}", "HotKey6") EndFunc Func HotKey7() HotKeySet ("{7}") $hktd = TimerDiff($hkt) If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Sleep ( 600 ) Send ("{7}") Else Send ("7") EndIf HotKeySet ("{7}", "HotKey7") EndFunc Func HotKey8() HotKeySet ("{8}") $hktd = TimerDiff($hkt) If $cmmode = False Then $override = True $cmmode = True Send ("9") $hkt = TimerInit() Else Send ("8") EndIf HotKeySet ("{8}", "HotKey8") EndFunc Func HotKey9() HotKeySet ("{9}") Send ("{9}") HotKeySet ("{9}", "HotKey9") EndFunc Func HotKey0() HotKeySet ("{0}") Send ("{0}") HotKeySet ("{0}", "HotKey0") EndFunc Func HotKey11() HotKeySet ("{-}") Send ("{-}") HotKeySet ("{-}", "HotKey11") EndFunc Func HotKey12() HotKeySet ("{=}") Send ("{=}") HotKeySet ("{=}", "HotKey12") EndFunc Edited April 10, 2015 by AlwaysLearning argumentum 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now