使用鼠标和键盘计算用户空闲时间 [英] User idle time calculation using mouse and keyboard

查看:272
本文介绍了使用鼠标和键盘计算用户空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写Windows服务,使用鼠标和键盘查找用户空闲时间,不使用5分钟的开始计数,然后返回使用鼠标/键盘停止计数,如每天24小时的计数,并发送给邮件负责人。 />


我尝试过:



我正在搜索C#和windows的代码服务。我没有得到代码而且不知道这个请帮助我任何人都有这个想法。

这个项目主要用于MNC公司的活动跟踪目的

解决方案

您可以使用此代码将其转换为c#。



 Option Strict On 


Imports System.Runtime.InteropServices

公共类IdleTime

公共结构LASTINPUTINFO
Public cbSize As UInteger
Public dwTime As UInteger
结束结构

'API调用让我们检测任何键盘和/或鼠标活动
< DllImport(user32.dll)> _
私有共享函数GetLastInputInfo(ByRef plii As LASTINPUTINFO)As Boolean
结束函数

'返回自上次用户活动以来的时间
Public Function GetInactiveTime()As Nullable (Of TimeSpan)
昏暗信息作为LASTINPUTINFO =新LASTINPUTINFO
info.cbSize = CUInt(Marshal.SizeOf(info))
If(GetLastInputInfo(info))然后
返回TimeSpan .FromMilliseconds(Environment.TickCount - info.dwTime)
Else
返回Nothing
结束如果
结束函数

结束类





在您的MainModule上添加此代码



 Private IdleTimer As IdleTime 





Form_Load



'检查自动锁定时间/ 10分钟默认
AutoLockTime = 600

如果AutoLockTime< = 0那么
InactivityTimer.Enabled = False'已禁用
否则
AutoLockTime = AutoLoc kTime + 10'加10秒津贴
InactivityTimer.Enabled = True'激活空闲检查
结束如果







添加计时器控件和文本控件



 Private Sub InactivityTimer_Tick(sender As System.Object ,e As System.EventArgs)处理InactivityTimer.Tick 

Application.DoEvents()

如果IS_BUSY_PROCESSING则退出Sub

'计算多长时间我们一直闲置
Dim inactiveTime = IdleTimer.GetInactiveTime

If(inactiveTime Is Nothing)或(AutoLockTime< = 0)然后

InactivityTimer.Enabled = False

ElseIf(inactiveTime.Value.TotalSeconds> 10)然后

Dim DisplayMinutes As Integer =(AutoLockTime - 10)/ 60
Dim DisplayTime As Integer = AutoLockTime - (inactiveTime.Value.TotalSeconds)
Text1.Text = String .Format(闲置在{0}分钟/秒,系统将自动锁定。{1}剩余时间。,DisplayMinutes,SecondsToHours(DisplayTime)))

如果inactiveTime.Value.TotalSeconds> ; = AutoLockTime然后
'执行屏幕锁定
PerformLockScreen()
结束如果

否则
'StatusBarMessage()
结束如果
End Sub







希望这会有所帮助! :)


I want to write windows service for find user idle time using a mouse and keyboard not using 5-minute start count and back to use mouse/keyboard stop count like count per day 24 hours and send to mail responsible person.

What I have tried:

I am searching code for C# and windows service. I am not getting the code and no idea for this please help me anyone have the idea for this.
This project mostly used for MNC company's for activity tracking purpose

解决方案

you can use this code just convert it to c#.

Option Strict On


Imports System.Runtime.InteropServices

Public Class IdleTime

    Public Structure LASTINPUTINFO
        Public cbSize As UInteger
        Public dwTime As UInteger
    End Structure

    'API call that let us detect any keyboard and/or mouse activity
    <DllImport("user32.dll")> _
    Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

    'Returns the time since last user activity
    Public Function GetInactiveTime() As Nullable(Of TimeSpan)
        Dim info As LASTINPUTINFO = New LASTINPUTINFO
        info.cbSize = CUInt(Marshal.SizeOf(info))
        If (GetLastInputInfo(info)) Then
            Return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime)
        Else
            Return Nothing
        End If
    End Function

End Class



On your MainModule add this codes

Private IdleTimer As IdleTime



Form_Load

'check autolock time / 10 minutes default
AutoLockTime = 600

If AutoLockTime <= 0 Then
    InactivityTimer.Enabled = False     'Disabled
Else
    AutoLockTime = AutoLockTime + 10    'Plus 10 Seconds Allowance
    InactivityTimer.Enabled = True      'Activate Checking of Idle
End If




Add timer control and text control

Private Sub InactivityTimer_Tick(sender As System.Object, e As System.EventArgs) Handles InactivityTimer.Tick

            Application.DoEvents()

            If IS_BUSY_PROCESSING Then Exit Sub

            'Calculates for how long we have been idle
            Dim inactiveTime = IdleTimer.GetInactiveTime

If (inactiveTime Is Nothing) Or (AutoLockTime <= 0) Then

                InactivityTimer.Enabled = False

            ElseIf (inactiveTime.Value.TotalSeconds > 10) Then

                Dim DisplayMinutes As Integer = (AutoLockTime - 10) / 60
                Dim DisplayTime As Integer = AutoLockTime - (inactiveTime.Value.TotalSeconds)
                Text1.Text = String.Format("Idle in {0} minute/s, the system will automatically lock. {1} time remaining.", DisplayMinutes, SecondsToHours(DisplayTime)))

                If inactiveTime.Value.TotalSeconds >= AutoLockTime Then
                    'perform screen lock
                    PerformLockScreen() 
                End If

            Else
                'StatusBarMessage()
            End If
        End Sub




hope this will help! :)


这篇关于使用鼠标和键盘计算用户空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆