Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 29-May-18 5:29am

1 solution

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! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900