Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Is there a (simple) way to detect user inactivity system wide? As a screensaver would do?

I know GetLastInputInfo, this seems to be exactly what I am looking for but unfortunately it is not system wide but only for the current process.

I want to write an extension for a password manager (KeePass) which locks the password file after a specific amount of user inactivity...

Thank you.

Regards,
Posted

Imports System.Runtime.InteropServices

Public Class IdleTime

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

    <structlayout(layoutkind.sequential)> _
    Public Structure LASTINPUTINFO
        Public cbSize As Int32
        Public dwTime As Int32
    End Structure


    Public ReadOnly Property IdleTime() As Integer

        Get

            Dim lastInput As New LASTINPUTINFO

            lastInput.cbSize = Marshal.SizeOf(lastInput)

            If GetLastInputInfo(lastInput) Then

                Return (Environment.TickCount - lastInput.dwTime) / 1000

            End If

        End Get

    End Property

End Class
 
Share this answer
 
Comments
Member 13156864 12-Jun-17 0:33am    
will this code helps for identifying the inactivity of the system to pause the running timer in the windows forms application?
Hi,

Thank you, but doesn't GetLastInputInfo() affect the current application?

This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function.


In fact my application is a tray application so it will not get any input from the user all the time so the solution should work for the whole system, not just the application.

Regards,
 
Share this answer
 
You didn't mention which language you want to do this in, but here's a nice C# article about this:

How to check for user inactivity with and without platform invokes in C#[^]
 
Share this answer
 
Thank you all.

I just misunderstood GetLastInputInfo().

I have now a window with SetTimer every few seconds where I just query GetLastInputInfo(). If the value is the same for a specific amount of time I just do my action...

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900