Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Win32 idle state (screensaver like)

by thenetfreaker (Friar)
on Sep 28, 2007 at 12:06 UTC ( [id://641514]=perlquestion: print w/replies, xml ) Need Help??

thenetfreaker has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monasterians,
I'm trying to write a script that will do a specific function when the computer shell be untouched for 15 minutes. I was wondering how to do that because after reading Win32::Process i couldn't find there anything that tells for 'how long' the computer been idle (just the timeout when to die). I found in the following post a small code to use Win32::API but it is unclear to me, Checking that local Windows machine is idle, and the monasterian that posted that code wasn't in the monastery for 4 years so there's no-one to ask in person.

I'm trying to do something that would look like :
#!/usr/bin/perl -w use strict; # use some Win32 module for the task # the checking if anything been pressed logically combined with perl s +eems to me to look like my $trials = 20; # for 10 minutes while ($trials) { if (("mouse moved") or ("any key pressed")) { $trials = 20; # start the countdown all over again } else { $trials--; #decrease a trial sleep(30); #wait half a minute } } #now that $trials equals to 0 - therefore nothing been pressed for 10 +minutes (20 halves of a minute {for better results}) doMyFunc($var1, $var2); sub doMyFunc { ... }

now my question is how to properly implement the part of the code where perl should determine the "idle state" of Win32, in which the user neither presses anything nor moves the mouse ?

Replies are listed 'Best First'.
Re: Win32 idle state (screensaver like)
by cdarke (Prior) on Sep 28, 2007 at 12:50 UTC
    One possibility would to install "yourself" as a screen-saver in the registry entry: \HKEY_CURRENT_USER\Desktop\SCRNSAVER.EXE
    Can't say I have tried this though.
    The API SystemParametersInfo might be of interest as well.
      In Win32, Screen Saver programs are not actually processes, and must have a window message loop (but not a *normal* window message loop, that would be too simple). It's not possible to use a console app as a screen saver, and it's often very hard to retrofit a regular Win32 windowing app to support the strange limitations that face screen savers.

      Separately, you then lose the ability to run a user-specified screen saver.

      Much more likely, you want your idle perl task to determine the "system load" in some other way, and start your work only when the system load is low for a suitable amount of time.

      --
      [ e d @ h a l l e y . c c ]

        Not quiet, the use can have tasks running, like trackers (e-Mule, torrents, etc), or an installation of some big program that takes a lot of time, or any other long task that requires respective CPU usage. Which means that i need to get from the system in some way information that no key was pressed and the mouse wasn't moved for a "suitable" amount of time.
        Like when ICQ drops to Away mode after a while that the PC wasn't touched.
      1. I don't understand how to use Win32::API for this purpose.
      2. Can i register myself as a screensaver via perl ?
Re: Win32 idle state (screensaver like)
by BrowserUk (Patriarch) on Sep 28, 2007 at 18:44 UTC
      Wonderful, now how do i combine it with perl ?
      though it's told there that it's also C++'s function i've never seen it in C++ neither in C# and most important, i don't know how to get it to return the output into a Perl's variable.

        You learn how to use Win32::API

        This uses win32::API::Prototype which you may not be able to obtain, but shoudl be readily adaptable to Win32::API. It loops, calling the api and printing the return. Move the mouse or touch a key to see the output change.

        #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'user32.dll', 'bool GetLastInputInfo( pvoid p )' ) or die $^E +; my $info = pack 'VV', 8, 0; while( sleep 1 ) { GetLastInputInfo( $info ) or die $^E; printf "Last input was at: %u\n", unpack 'x[V] V', $info; } __END__ c:\test>junk Last input was at: 850068125 Last input was at: 850068125 Last input was at: 850068125 Last input was at: 850068125 Last input was at: 850068125 Last input was at: 850073953 Last input was at: 850073953 Last input was at: 850073953 Last input was at: 850073953 Last input was at: 850077828 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850078250 Last input was at: 850086593 Last input was at: 850086593 Last input was at: 850086593 Last input was at: 850086593 Last input was at: 850090578 Last input was at: 850092078 Last input was at: 850092703 Terminating on signal SIGINT(2)

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://641514]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found