dufran3 Posted May 9, 2011 Share Posted May 9, 2011 (edited) This script works well for me at the moment. Is there anyway that I can optimize it so that it uses less system resources? expandcollapse popup#include <Timers.au3> #include <Constants.au3> #NoTrayIcon ;Screen Timeout Countermeasure Opt("TrayOnEventMode",1) Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown. HotKeySet('{ESC}', '_Terminate') $exit = TrayCreateItem("Exit") TrayItemSetOnEvent(-1,"_Terminate") TraySetState() $aMouseOrigPos = MouseGetPos() _MoveMouse($aMouseOrigPos) Func _MoveMouse($aMouseOrigPos) While 1 If @error = -1 Then ExitLoop EndIf $timer = _Timer_GetIdleTime() If $timer > 500000 Then ConsoleWrite('---------------------' & @LF) $aMouseCurrentPos = MouseGetPos() If $aMouseCurrentPos[0] == $aMouseOrigPos[0] Then $aMousePosition = MouseGetPos() ConsoleWrite('$aMousePosition = ' & $aMousePosition[0] & ',' & $aMousePosition[1] & @LF) MouseMove($aMousePosition[0] +1,$aMousePosition[1] +1) ConsoleWrite('Moving mouse ' & $aMousePosition[0] +1 & ',' & $aMousePosition[1] +1 & @LF) Else ConsoleWrite('MouseGetPos does not = '& $aMouseOrigPos[0] & ',' & $aMouseOrigPos[1] & @LF) MouseMove($aMouseOrigPos[0],$aMouseOrigPos[1]) ConsoleWrite('Moving mouse ' & $aMouseOrigPos[0] & ',' & $aMouseOrigPos[1] & @LF) EndIf EndIf Sleep(500000) Wend EndFunc Func _Terminate() Exit EndFunc ;==>_Terminate Edit: looking at my script, I notice a flaw I think. I am looking for the timer to be > 500000, then I am sleeping for 500000. so if the timer is 499999, I will sleep for 500000, causing the script to fail, because it won't check the timer again until the timer is 999999, which is not what I want. Maybe I can take the difference, of timer versus the time I am checking and sleep for that long? Edited May 9, 2011 by dufran3 Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 How about this instead? expandcollapse popup#include <Timers.au3> #include <Constants.au3> #NoTrayIcon ;Screen Timeout Countermeasure Opt("TrayOnEventMode",1) Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown. HotKeySet('{ESC}', '_Terminate') $exit = TrayCreateItem("Exit") TrayItemSetOnEvent(-1,"_Terminate") TraySetState() $aMouseOrigPos = MouseGetPos() _MoveMouse($aMouseOrigPos) Func _MoveMouse($aMouseOrigPos) While 1 If @error = -1 Then ExitLoop EndIf $timer = _Timer_GetIdleTime() ConsoleWrite('timer = ' & $timer & @LF) ;<---Just added $SleepTimer = 840000 - $timer ;<---Just added If $timer > 840000 Then ConsoleWrite('---------------------' & @LF) $aMouseCurrentPos = MouseGetPos() If $aMouseCurrentPos[0] == $aMouseOrigPos[0] Then $aMousePosition = MouseGetPos() ConsoleWrite('$aMousePosition = ' & $aMousePosition[0] & ',' & $aMousePosition[1] & @LF) MouseMove($aMousePosition[0] +1,$aMousePosition[1] +1) ConsoleWrite('Moving mouse ' & $aMousePosition[0] +1 & ',' & $aMousePosition[1] +1 & @LF) Else ConsoleWrite('MouseGetPos does not = '& $aMouseOrigPos[0] & ',' & $aMouseOrigPos[1] & @LF) MouseMove($aMouseOrigPos[0],$aMouseOrigPos[1]) ConsoleWrite('Moving mouse ' & $aMouseOrigPos[0] & ',' & $aMouseOrigPos[1] & @LF) EndIf EndIf ConsoleWrite('Sleeping for ' & $SleepTimer & @LF) ;<---Just added Sleep($SleepTimer) ;<---Just added Wend EndFunc Func _Terminate() Exit EndFunc ;==>_Terminate Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 9, 2011 Moderators Share Posted May 9, 2011 dufran3,I would use Adlib to only run the function when it was needed. This moves the mouse every second in alternate directions: HotKeySet("{ESC}", "On_Exit") AdlibRegister("_MoveMouse", 1000) ; Every second for testing Global $fToggle = True While 1 Sleep(10) WEnd Func _MoveMouse() ; Toggle direction $fToggle = Not $fToggle ; get position of mouse $aMPos = MouseGetPos() ; Move mouse MouseMove($aMPos[0] + (2 * $fToggle - 1), $aMpos[1]) ; Announce for testing ConsoleWrite("Mouse Moved" & @CRLF) EndFunc Func On_Exit() Exit EndFuncAny help? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 dufran3, I would use Adlib to only run the function when it was needed. This moves the mouse every second in alternate directions: HotKeySet("{ESC}", "On_Exit") AdlibRegister("_MoveMouse", 1000) ; Every second for testing Global $fToggle = True While 1 Sleep(10) WEnd Func _MoveMouse() ; Toggle direction $fToggle = Not $fToggle ; get position of mouse $aMPos = MouseGetPos() ; Move mouse MouseMove($aMPos[0] + (2 * $fToggle - 1), $aMpos[1]) ; Announce for testing ConsoleWrite("Mouse Moved" & @CRLF) EndFunc Func On_Exit() Exit EndFunc Any help? M23 Here is the problem. I am writing this script for folks in my office, and I don't want the mouse to move if they are using their computer. Your script, while wayyyy simpler and streamlined, will move the mouse on a count and not take into account user input. Which is something I need to consider. Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 (edited) expandcollapse popup#include <Timers.au3> #NoTrayIcon Opt("TrayOnEventMode",1) Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown. $exit = TrayCreateItem("Exit") TrayItemSetOnEvent(-1,"On_Exit") TraySetState() HotKeySet("{ESC}", "On_Exit") AdlibRegister("_MoveMouse", 1000) ; Every second for testing Global $fToggle = True While 1 Sleep(10) WEnd Func _MoveMouse() $timer = _Timer_GetIdleTime() If $timer > 870000 Then ; Toggle direction $fToggle = Not $fToggle ; get position of mouse $aMPos = MouseGetPos() ; Move mouse MouseMove($aMPos[0] + (2 * $fToggle - 1), $aMpos[1]) ; Announce for testing ConsoleWrite("Mouse Moved" & @CRLF) EndIf EndFunc Func On_Exit() Exit EndFunc How about this? took what you gave me and added a timer and an If statement...do you see any problems with this? Edit: Is there a way to test which runs using less system resources? Edited May 9, 2011 by dufran3 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 9, 2011 Moderators Share Posted May 9, 2011 dufran3, I would certainly set the times to something more realistic - a single second seems a little short! You might also want to shorten the code a little: $timer = _Timer_GetIdleTime() If $timer > 1000 Then ; Just use the value directly If _Timer_GetIdleTime() > 1000 Then Is there a way to test which runs using less system resources?Look in Task Manager? But be careful interpreting the results - I seem to remember that Sleep can appear to use quite a lot of CPU when nothing else is running, but then really sleeps properly when there is a heavy load elsewhere. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 (edited) dufran3, I would certainly set the times to something more realistic - a single second seems a little short! You might also want to shorten the code a little: $timer = _Timer_GetIdleTime() If $timer > 1000 Then ; Just use the value directly If _Timer_GetIdleTime() > 1000 Then Look in Task Manager? But be careful interpreting the results - I seem to remember that Sleep can appear to use quite a lot of CPU when nothing else is running, but then really sleeps properly when there is a heavy load elsewhere. M23 I need to get out of that bad habit of assigning variables when i don't need to. Thanks for that. I will take a look at task manager after compiling them both to .exe and see what it says.thanks melba Edit: I fail... My Timeout: 4,212K Timeout_Melba: 4,016K Edited May 9, 2011 by dufran3 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 9, 2011 Moderators Share Posted May 9, 2011 dufran3,My pleasure. When you reply please try to use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 Ok, I will remember that. Can you please explain a bit of your black magic to me? It took me quite some time to figure out how to get the mouse to move back and forth, so that it wouldn't ever go off screen. I don't understand the toggle in your script, and how it is working. Can you help me with that? I would have never even thought of that. $fToggle = Not $fToggle $aMPos = MouseGetPos() MouseMove($aMPos[0] + (2 * $fToggle - 1), $aMpos[1]) Getting the mouse position, then moving the mouse x position + 2 * $fToggle - 1(huh?), mouse y position. It is that toggle section that is confusing me... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 9, 2011 Moderators Share Posted May 9, 2011 dufran3,$fToggle is a Boolean variable - it is either True or False. Each time we call the function, we toggle the value by using Not - as Not True = False and vice versa. The sneaky bit is using the variable as a numeric value. AutoIt regards True as 1 and False as 0 - although you should not depend on this as the Help file for these values clearly states: "These keywords should not be used in expressions as AutoIt will not detect this 'misuse' and the results will be unpredictable." However, it has always worked for me up to now - there is just no guarantee that it will in future releases. So we can do a bit of arithmetic trickery using these numeric values:Case $fToggle = True -> (2 * $fToggle - 1) = (2 * 1 - 1) = 2 - 1 = 1 Case $fToggle = False -> (2 * $fToggle - 1) = (2 * 0 - 1) = 0 - 1 = -1So each time we call the function we move the mouse 1 pixel in the reverse direction to the previous time. We could also do it "legally" like this:HotKeySet("{ESC}", "On_Exit") AdlibRegister("_MoveMouse", 1000) ; Every second for testing Global $iDirection = 1 While 1 Sleep(10) WEnd Func _MoveMouse() ; Toggle direction If $iDirection = 1 Then $iDirection = 0 Else $iDirection = 1 EndIf ; get position of mouse $aMPos = MouseGetPos() ; Move mouse MouseMove($aMPos[0] + ((2 * $iDirection) - 1), $aMpos[1]) ; Announce for testing ConsoleWrite("Mouse Moved" & @CRLF) EndFunc Func On_Exit() Exit EndFuncAll clear now? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dufran3 Posted May 9, 2011 Author Share Posted May 9, 2011 Crystal clear. Nice work! 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