Solved

How To Read The Screensaver Timer

Posted on 2006-07-03
7
228 Views
Last Modified: 2008-02-01
Hello,

In VB 6, running under Windows 2000 or XP, how can one read the timer that the screensaver uses to determine how long it has been since the last user-generated activity. I realize a VB program could have its own timer, but ideally this program would not need to be running constantly, as a screensaver would. Rather it could execute, report (e.g. write to a log file), and close, whether running from the task scheduler, or remotely.

If the same could be accomplished with a .bat file, without resorting to VB, that would be just as good, if not better.

Thanks in advance,

s1m0ne
0
Comment
Question by:s1m0ne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 100 total points
ID: 17034384
0
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 100 total points
ID: 17036546
Another approach is to get the screen saver timeout value. Once your armed with this value you can then use BeginIdleDetection API.  Since SystemParametersInfo API gives the screen saver timeout in seconds and BeginIdleDetection uses minutes we need to simply convert the seconds to minutes then pass it to our procedure. You might be notified 2-3 seconds early about when the screen saver is about to run, this is simply due to the messaging going on in the background. Basically as you pass the minute into the procedure it will detect when you become idle and wait for that duration. Once the duration is up it singnals the callback procedure. This callback fires once per action meaning it will poll endlessley about when you become idle and when you become active again, each time reseting itself.

'-------- Module1.bas ----------

Option Explicit

'Msidle define
Private Const USER_IDLE_BEGIN = 1
Private Const USER_IDLE_END = 2

'SystemParametersInfo define
Private Const SPI_GETSCREENSAVETIMEOUT = 14

Private Declare Function BeginIdleDetection Lib "Msidle.dll" Alias "#3" ( _
    ByVal pfnCallback As Long, _
    ByVal dwIdleMin As Long, _
    ByVal dwReserved As Long) As Long

Private Declare Function EndIdleDetection Lib "Msidle.dll" Alias "#4" ( _
    ByVal dwReserved As Long) As Long

Private Declare Function SystemParametersInfoA Lib "user32" ( _
    ByVal uAction As Long, _
    ByVal uParam As Long, _
    ByRef lpvParam As Any, _
    ByVal fuWinIni As Long) As Long

Public Function ScreenSaverTime() As Long
    Dim SvrSeconds  As Long
    SystemParametersInfoA SPI_GETSCREENSAVETIMEOUT, 0&, SvrSeconds, 0&
    ScreenSaverTime = SvrSeconds
End Function

Public Sub BeginIdle(IdleMin As Long)
    If BeginIdleDetection(AddressOf IdleCallBack, IdleMin, 0&) = 0 Then
        Debug.Print "Success - BeginIdleDetection"
    End If
End Sub

Public Sub EndIdle()
    If EndIdleDetection(0&) Then
        Debug.Print "Success - EndIdleDetection"
    End If
End Sub

Public Sub IdleCallBack(ByVal dwState As Long)
    Select Case dwState
        Case USER_IDLE_BEGIN
           Form1.List1.AddItem "Screen saver activated ---  " & Time
        Case USER_IDLE_END
           Form1.List1.AddItem "Screen saver deactivated ---  " & Time
    End Select
End Sub



'------- Form1 --------

'Add 1 Listbox
'Add 1 Command button
'When you close the form close it by X button and not the VB IDE stop button.

Option Explicit

Private Sub Command1_Click()
   
    Dim arg As Long
   
    arg = ScreenSaverTime   ' get screen saver run time
    arg = (arg / 60)        ' seconds to minutes for idle
    BeginIdle arg           ' set up the callback to let us know
   
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    EndIdle                 ' stop callback and idle detection
End Sub
0
 
LVL 41

Expert Comment

by:graye
ID: 17039345
I'd agree that the GetLastInputInfo API is the way to go, but you won't be able to do this remotely... it must be run from the local PC
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:s1m0ne
ID: 17125621
Thanks. Sorry for the (continuing) delay. I got the first example working. I'll try to do the second one in the next couple days, for comparison. Ideally, something like this could be run as a service, but that may be beyond the scope of this question.
0
 
LVL 1

Author Comment

by:s1m0ne
ID: 17265471
Still working on it ...
0
 
LVL 1

Author Comment

by:s1m0ne
ID: 17657423
Sorry for letting this drag on so long. I'm splitting the points evenly between BlueDevilFan and egl1044. Thanks to all.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question