Jump to content

Screen Timeout Script


Recommended Posts

This script works well for me at the moment. Is there anyway that I can optimize it so that it uses less system resources?

#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 by dufran3
Link to comment
Share on other sites

How about this instead?

#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

  • Moderators

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? :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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? :unsure:

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

#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 by dufran3
Link to comment
Share on other sites

  • Moderators

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? :unsure: 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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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? :unsure: 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 by dufran3
Link to comment
Share on other sites

  • Moderators

dufran3,

My pleasure. :unsure:

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

  • Moderators

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

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 = -1

So each time we call the function we move the mouse 1 pixel in the reverse direction to the previous time. :unsure:

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
EndFunc

All clear now? :>

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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