VB Detect Idle time
Asked Answered
N

2

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
Noncompliance answered 28/9, 2012 at 9:2 Comment(2)
Is your goal to detect keyboard/mouse activity outside of your application?Nonproductive
Yes kinda and if no activity is detected then run a command // codeNoncompliance
B
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.

Bergamot answered 28/9, 2012 at 13:53 Comment(0)
W
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
Wellfavored answered 4/8, 2022 at 11:39 Comment(1)
Please do not post an answer with untested code. If the code can not be tested because the original question doesn't include a minimum example, ask for one ( stackoverflow.com/help/minimal-reproducible-example ).Pervert

© 2022 - 2024 — McMap. All rights reserved.