Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect inactivity 2

Status
Not open for further replies.

fischadler

Programmer
May 31, 2002
258
MT
Is it possible for a VB application to detect a said amount of time of Windows inactivity? Kind of like how the Windows screen saver works.
The program should be running all the time but it should do an action after, say, 30 minutes of inactivity (not just on the program, but on the whole system)
 
I would imagine you are looking at hooking messages from the API, but that's not my area of expertise.

Strongm is someone who knows this stuff inside out - so I'd just leave it and wait to see if he wonders this way.

mmilan
 
check out thread222-859727, it may be of some help. A search for 'inactivity' or 'inactive' will produce other results.

zemp
 
Windows 98, but it would help if I could also do it in Win XP. My development machine is an XP, but the final product should work on Win 98.
 
I had already checked thread 222-859727 before posting this question, but it does not answer my problem. I want to detect windows inactivity, not just that of my app.
 
W98, huh? Shame. I don't know of a straightforward way of doing this in VB on W98. The hooking mentioned by mmilan isn't possible from VB on the W9x/Me platforms as they do not support the necessary low-level keyboard and mouse hooks (the systemwide hooks they do support cannot be used directly from VB, gnerally requiring a C/C++ stub DLL).

Well, not quite true. You could probably use a journalling hook, but that would adversely affect system performance since you'd be intercepting every message. And I have an idea that involves polling for the current foreground window, then attaching to the owning thread's input message queue...however, I no longer have anything less than W2K on any of my machines, so I can't test it.

If your target were W2K, XP or W2K3 then there are a number of pretty simple solutions. Oh well...
 
I'll try to convince the end user to upgrade to Win XP. If I do, I'll be posting this question again, unless you want to tell me how to do it in Win XP now.
A star goes to you anyway. Thanks!
 
Here's a suggested solution for W2K/XP/W2K3. You'll need a form with a timer and a command button:
Code:
[COLOR=blue]Option Explicit

Private Type LASTINPUTINFO
        cbSize As Long
        dwTime As Long
End Type

Private Declare Function GetLastInputInfo Lib "user32" (li As LASTINPUTINFO) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private mvarIdleTime As Long

Private Sub Command1_Click()
    DetectIdle 5000, 500
End Sub


Private Sub Timer1_Timer()
    If GetTickCount - GetInputTick > mvarIdleTime Then
        MsgBox "IDLE for " & CStr(GetTickCount - GetInputTick) & "ms"
    End If
End Sub

Private Sub DetectIdle(ByVal lIdleTime As Long, ByVal lTimerInterval As Long)
    Timer1.Interval = lTimerInterval
    Timer1.Enabled = True
    mvarIdleTime = lIdleTime
End Sub

' Returns system tick count when last input occurred
Private Function GetInputTick() As Long
    Static LastInputTick As Long
    
    Dim myLI As LASTINPUTINFO
    myLI.cbSize = Len(myLI)
    GetLastInputInfo myLI
    GetInputTick = myLI.dwTime
End Function[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top