9

I'm looking for a way to detect if the user has been idle for 5 min then do something, and if and when he comes back that thing will stop, for example a timer.

This is what i have tried (but this will only detect if form1 has been inactive / not clicked or anything):

Public Class Form1

Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'You should have already set the interval in the designer... 
    Timer1.Start()
End Sub

Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    Timer1.Stop()
    Timer1.Start()
End Sub


Private Sub form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    Timer1.Stop()
    Timer1.Start()
End Sub

Private Sub form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    Timer1.Stop()
    Timer1.Start()
End Sub

Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    MsgBox("Been idle for to long") 'I just have the program exiting, though you could have it do whatever you want.
End Sub

End Class
2
  • 2
    Is your goal to detect keyboard/mouse activity outside of your application?
    – Rob P.
    Sep 28, 2012 at 9:08
  • 1
    Yes kinda and if no activity is detected then run a command // code Sep 28, 2012 at 9:14

2 Answers 2

17

This is done easiest by implementing the IMessageFilter interface in your main form. It lets you sniff at input messages before they are dispatched. Restart a timer when you see the user operating the mouse or keyboard.

Drop a timer on the main form and set the Interval property to the timeout. Start with 2000 so you can see it work. Then make the code in your main form look like this:

Public Class Form1
    Implements IMessageFilter

    Public Sub New()
        InitializeComponent()
        Application.AddMessageFilter(Me)
        Timer1.Enabled = True
    End Sub

    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        '' Retrigger timer on keyboard and mouse messages
        If (m.Msg >= &H100 And m.Msg <= &H109) Or (m.Msg >= &H200 And m.Msg <= &H20E) Then
            Timer1.Stop()
            Timer1.Start()
        End If
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        MessageBox.Show("Time is up!")
    End Sub
End Class

You may have to add code that disables the timer temporarily if you display any modal dialogs that are not implemented in .NET code.

0
0

This might work by setting it to just call the Reset idk i just want it work all over the program idk how to do it, i just created this code :


    Public Class test
    Dim IdleTimer As String
    Dim testsave As String
    Dim idle_TimerSet As String = 60 '<---- Here You choose The timer (1 per Sec)


    Private Sub test_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        IdleTimer = idle_TimerSet

    End Sub

    Private Sub Idle_Tick(sender As Object, e As EventArgs) Handles Idle.Tick
        If IdleTimer <= 1 Then

            MsgBox("[ Idle Screen ]")
        Else

            IdleTimer = IdleTimer - 1     '<--- The Counter
            IdleTracker.Text = IdleTimer

        End If
    End Sub

    Private Sub test_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        Call Reset_Idle() 'This is on the main Form
    End Sub

    Private Sub test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Call Reset_Idle() 'This is on the main Form
    End Sub

    Public Sub Reset_Idle()   '<-- The Reset Action

        'Idle.Enabled = False
        IdleTimer = idle_TimerSet
        'Idle.Enabled = True

    End Sub
 End Class
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.