Capturing user interaction

D

Diana Mueller

Hello NG,

in my MDI-App, I'd like to checkfor how long the User hasn't done anything
(moved the mouse, pressed a key) so that I can do some stuff after a given
amount of idle time. My first idea was to save the current time in a
variable every time the user moves the mouse or presses a key so that i can
use that variable to calculate idle time.

I ran into a problem at the very start though. The MouseMove Event of my MDI
parent form is never fired. Is that standard behaviour for MDI containers?
What would be an elegant way to capture the point in time of the last user
interaction?
 
D

Diana Mueller

Solved it myself. If anyone ever has to do this:
(works with >= Win2K)

Imports System.Runtime.InteropServices

Public Class IdleTracker

Private Declare Function GetLastInputInfo Lib "User32.dll" (ByRef lii As
LASTINPUTINFO) As Boolean

<StructLayout(LayoutKind.Sequential)> _

Public Structure LASTINPUTINFO

Public cbSize As Int32

Public dwTime As Int32

End Structure

Public Shared ReadOnly Property IdleTimeInTicks() As Int32

Get

Dim lii As New LASTINPUTINFO

lii.cbSize = Marshal.SizeOf(lii)

If GetLastInputInfo(lii) Then

Return Environment.TickCount - lii.dwTime

End If

End Get

End Property

End Class
 
G

Guest

I have not tried what I am about to suggest, so some experimentation is
called for.
Add a timer and an Application.Idle event handler to your app. In the idle
event handler, enable the timer to Now plus an interval of your choice. When
the timer event fires, your app has experienced the desired period of
idleness. So, disable the timer and do whatever you want to do.

Be careful, though. I once had a bad experience with the Idle event. I was
trying to defer painting a window which was time consuming and yielded
flicker and delays. I got tangled up validating and invalidating windows and
I blamed it on the Idle event - doubtless the fault was mine, but I couldn't
untangle myself. Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top