Sign in to follow this  
Followers 0
txomin

How to terminate the script like a Screen Saver does

11 posts in this topic

#1 ·  Posted (edited)

Hi everybody,

I would like to write some sort of screen saver app, but I'm not sure what would be the best way to have my script to terminate when the user:

- moves the mouse

OR

- press a key on the keyboard or mouse

Any suggestions?

Edited by txomin

Share this post


Link to post
Share on other sites



Hi everybody,

I would like to write some sort of screen saver app, but I'm not sure what would be the best way to have my script to terminate when the user:

- moves the mouse

OR

- press a key on the keyboard or mouse

Any suggestions?

write a while loop to check if the mouse moved or some key pressed, is it happened, exit the script, it seems not very difficult to do what you need

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

write a while loop to check if the mouse moved or some key pressed, is it happened, exit the script, it seems not very difficult to do what you need

The only thing I have is to exit on mouse move:

While 1
 $mpos = MouseGetPos()
 Sleep(200)
 if $mpos <> MouseGetPos() then Exit
Wend

How do I know if there has been a Mouse click? (A function MouseGetClick does not exit)

How do I know if user typed on the keyboard?

What about the MouseWheel?

Anybody knows?

Edited by txomin

Share this post


Link to post
Share on other sites

The only thing I have is to exit on mouse move:

While 1
 $mpos = MouseGetPos()
 Sleep(200)
 if $mpos <> MouseGetPos() then Exit
Wend

How do I know if there has been a Mouse click? (A function MouseGetClick does not exit)

How do I know if user typed on the keyboard?

What about the MouseWheel?

Anybody knows?

remember the last position of you mouse, and use MouseGetPos to check whether the position has changed

to determine whether some key pressed, look into the function _IsPressed

Share this post


Link to post
Share on other sites

How do I know if there has been a Mouse click? (A function MouseGetClick does not exit)

How do I know if user typed on the keyboard?

What about the MouseWheel?

Anybody knows?

About the mouse wheel, here is a little code I have produced myself.

GUIRegisterMsg(0x020A ,"CheckMouseWheel")

Func CheckMouseWheel($hWndGUI, $MsgID, $WParam, $LParam)
    If $WParam = 0x00780000 Then
            ;MouseWheel Up
    ElseIf $WParam = 0xFF880000 Then
            ;MouseWheel Down
    EndIf
EndFunc

I'm not sure about the values, it worked on my Win XP Home SP2.

Hope it can help you :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yep,

I saw _IsPressed, but it allows a single key check:

include <Misc.au3>
$dll = DllOpen("user32.dll")

While 1
    Sleep ( 250 )
    If _IsPressed("23", $dll) Then
        MsgBox(0,"_IsPressed", "End Key Pressed")
        ExitLoop
    EndIf
WEnd

DllClose($dll)

I would like to check for all keys at the same time...

Anyway, if I don't find anything better I would use a For cicle inside the While cicle... hopefully the cpu usage will not increase too much

Edited by txomin

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Just to share with the community...

#include <Misc.au3>

Func ExitOnSSEvent()
$dll = DllOpen("user32.dll")
$mposx = MouseGetPos(0)
$mposy = MouseGetPos(1)
While 1
    Sleep(50)
    If ($mposx <> MouseGetPos(0)) or ($mposy <> MouseGetPos(1)) Then ExitLoop
    For $i= 1 to 165
        If _IsPressed(Hex ($i,2), $dll) Then ExitLoop
    Next
Wend
DllClose($dll)
EndFunc

ExitOnSSEvent()

Please tell me if you find something should be different from your perspective.

Thank you :)

Edited by txomin

Share this post


Link to post
Share on other sites

Hey, just found this thread from your bug report actually, but I had something I wanted to add.

I created this function, it's a modified form of a function that I believe Gary wrote.

Sleep(1000) ; Wait a second before getting the lastactive otherwise the script detects movement/keypress and exits right away

Global $i_LastActive = _LastActive()
While 1
    ToolTip(@SEC) ; So you can see the script is active
    Sleep(1)
    If $i_LastActive <> _LastActive() Then ExitLoop
WEnd

Func _LastActive($v_User32Dll = 'user32.dll')
    Local $str_LastInput = DllStructCreate('uint;dword')
    DllStructSetData($str_LastInput, 1, DllStructGetSize($str_LastInput))
    DllCall($v_User32Dll, 'int', 'GetLastInputInfo', 'ptr', DllStructGetPtr($str_LastInput))

    Return DllStructGetData($str_LastInput, 2)
EndFunc

Share this post


Link to post
Share on other sites

Hey, just found this thread from your bug report actually, but I had something I wanted to add.

I created this function, it's a modified form of a function that I believe Gary wrote.

Sleep(1000) ; Wait a second before getting the lastactive otherwise the script detects movement/keypress and exits right away

Global $i_LastActive = _LastActive()
While 1
    ToolTip(@SEC) ; So you can see the script is active
    Sleep(1)
    If $i_LastActive <> _LastActive() Then ExitLoop
WEnd

Func _LastActive($v_User32Dll = 'user32.dll')
    Local $str_LastInput = DllStructCreate('uint;dword')
    DllStructSetData($str_LastInput, 1, DllStructGetSize($str_LastInput))
    DllCall($v_User32Dll, 'int', 'GetLastInputInfo', 'ptr', DllStructGetPtr($str_LastInput))

    Return DllStructGetData($str_LastInput, 2)
EndFunc
I must say that this script is even better!

Thank you Saunders :)

Share this post


Link to post
Share on other sites

just a suggestion, I would have one script be the screensaver and have a second one (a gui) actually do the detecting of mouse movement or keystroke to terminate the other, or to activate it if it doesn't detect anything after a while or the user initiates it.


[u]You can download my projects at:[/u] Pulsar Software

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  
Followers 0