Link to home
Start Free TrialLog in
Avatar of jes12345
jes12345

asked on

Find time since last user input in VB .NET

Does anyone know a way to find the time since the last user input (mouse or keyboard). I do not want this to effect system performance so I do not think it is a good idea to capture all user input so I am hoping someone knows about an alternative way. Thanks J
Avatar of kaufmed
kaufmed
Flag of United States of America image

VB Translation:
<DllImport("user32.dll")> _
Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
 
Friend Structure LASTINPUTINFO 
    Public cbSize As UInt;
    Public dwTime As UInt;
End Structure

Open in new window

Avatar of jes12345
jes12345

ASKER

In order to get it to work I had to put the code in a module. However the objects time is 0 even after the sleep.
Module Module1
    Public Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function
 
    Friend Structure LASTINPUTINFO
        Public cbSize As Integer
        Public dwTime As Integer
    End Structure
End Module
 
'AND
 
Imports System.Runtime.InteropServices
Imports System.Text
 
Public Class Form1
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        System.Threading.Thread.Sleep(15000)
 
        Dim test As New LASTINPUTINFO
        GetLastInputInfo(test)
        MsgBox(test.dwTime)
    End Sub
End Class

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I understand correctly, this only applies to your application; it is not system wide.
Sorry kaufmed but I did not understand your last question. Can I please ask you to elaborate?
Thanks J
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok I understand.Thank you for your replies. What I need is really a systemwide function. From reading the article it suggest to create a service using GetLastInputInfo() but that will not be possible on the design. Can you think of any other way of obtaining this? - if not I will find another design approach if installing a service is not an option.
Thanks J
You could implement some low-level hooks in conjunction with a timer. The low-level hooks will intercept the keyboard/mouse interactions and with each interception, you could restart the timer.
I guess that would be an option - do you think this would be ok with regards to system performance? Thank you so much for all your help and advises. Before I close this thread and assign the points can I please ask if you have an example of an efficient way of caturing key/mouse system wide? Thanks J
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic - I will definately be able to use this for my solution. Thank you so much for all your advises and help kaufmed! J
Good answer!