VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



PC Idle Monitor

by Jeremy (5 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

This module allows you to determine how long your computer has been Idle. It checks to see if a key was pressed or if the mouse has moved. Well commented.

Assumes
I hold all copy rights for this code you may distribute it as is. And may not be quoted as your own work so DON'T!
Code Returns
When you call the CheckIdleState Function Make sure you reset the CheckIdleState back to 0 so that It doesn't keep adding to the current value that you used in your code to perform a task.
API Declarations
In code below!

Rate PC Idle Monitor

Option Explicit
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
  X As Long
  Y As Long
End Type
Public Function CheckIdleState()As String
Dim kKey As Integer 'Stores each Key on the keyboard in the for next loop
Dim CurrentMousePos As POINTAPI 'Used to store the current mouse position
Static OldMousePos As POINTAPI 'Static-keeps the old mouse position
Static IdleTime As Date   'Stores the time in a date variable
Dim SystemIdle As Boolean  'Stores weather the systme is idle or not
SystemIdle = True 'Sets the idle value to true
For kKey = 1 To 256 'steps through each key on the keyboard it detect if
 If GetAsyncKeyState(kKey) <> 0 Then 'any of the keys have been pressed
  Debug.Print "Key Pressed"
  SystemIdle = False 'Sets the idle value to false
  Exit For 'Exits the for next loop so that it will move on to the next step
 End If
Next
GetCursorPos CurrentMousePos 'Gets the current cursor position and stores it
If CurrentMousePos.X <> OldMousePos.X Or _
CurrentMousePos.Y <> OldMousePos.Y Then 'Checks to see if the cursor has moved
  Debug.Print "Mouse Moved"
  SystemIdle = False    'since the last time it was checked
End If
OldMousePos = CurrentMousePos 'Stores the current mouse position for comparring positons the
        'next time through
If SystemIdle = True Then 'If a key hasn't been pressed and the mouse hasn't moved
 If DateDiff("s", IdleTime, Now) >= 60 Then 'it sets the return value to the elapsed time value
  IdleTime = Now 'Resets the time to check the next minute for idle
  CheckIdleStaate = CheckIdleState + 1 'sets the return value in minutes of being idle
 End If
Else
 IdleTime = Now 'Sets the new Current Idle Time to check for elapsed time
End If
End Function

Download this snippet    Add to My Saved Code

PC Idle Monitor Comments

No comments have been posted about PC Idle Monitor. Why not be the first to post a comment about PC Idle Monitor.

Post your comment

Subject:
Message:
0/1000 characters