VB 检测空闲时间



我正在寻找一种方法来检测用户是否已经空闲了 5 分钟,然后做某事,如果他回来,那件事就会停止,例如计时器。

这是我尝试过的(但这只会检测 form1 是否处于非活动状态/未单击或任何内容):

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

通过在主窗体中实现 IMessageFilter 接口,最简单的方法是完成此操作。 它允许您在调度输入消息之前嗅探它们。 当您看到用户操作鼠标或键盘时,重新启动计时器。

在主窗体上放置一个计时器,并将 Interval 属性设置为超时。 从 2000 开始,这样您就可以看到它的工作原理。 然后使主窗体中的代码如下所示:

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

如果显示任何未在 .NET 代码中实现的模式对话框,则可能必须添加暂时禁用计时器的代码。

这可能可以通过将其设置为仅调用重置idk来工作,我只是希望它在整个程序中idk中工作 如何做到这一点,我刚刚创建了这段代码:


    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

相关内容

  • 没有找到相关文章

最新更新