Does anyone know a way to tell how long your computer has been idle? Windows 2000 has an API for this, but 9*/NT do not as far as I know. Thanks in advance!
Ryan
[email protected]
Printable View
Does anyone know a way to tell how long your computer has been idle? Windows 2000 has an API for this, but 9*/NT do not as far as I know. Thanks in advance!
Ryan
[email protected]
Add a Timer to a Form:
------------------Code:Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static tTimer As Single
Static tLastPos As POINTAPI
Dim tCurPos As POINTAPI
Dim iKey As Integer
Timer1.Enabled = False
If tLastPos.x = 0 And tLastPos.y = 0 Then
'First Time, Setup the Variables
tTimer = Timer
Call GetCursorPos(tLastPos)
End If
'Check for Activity
Call GetCursorPos(tCurPos)
If tCurPos.x <> tLastPos.x Or tCurPos.y <> tLastPos.y Then
'Mouse Movement
tTimer = Timer
Else
For iKey = 1 To 255
If GetAsyncKeyState(iKey) Then Exit For
Next
If iKey > 255 Then
'No Activity, Idling
Caption = "Idle Time: " & Int(Timer - tTimer) & " Seconds."
Else
'Key/Mouse Button Pressed
tTimer = Timer
End If
End If
tLastPos.x = tCurPos.x
tLastPos.y = tCurPos.y
Timer1.Enabled = True
End Sub
Aaron Young
Analyst Programmer
[email protected]
[email protected]
I was searching for code to give the system idle time, and a lot of the posts I found were never resolved, so for future reference, this code works great, after replacing "<>" with "<>" and ">" with ">".
Just an FYI.
Here it is, edited:VB Code:
Private Type POINTAPI x As Long y As Long End Type Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Static tTimer As Single Static tLastPos As POINTAPI Dim tCurPos As POINTAPI Dim iKey As Integer Timer1.Enabled = False If tLastPos.x = 0 And tLastPos.y = 0 Then 'First Time, Setup the Variables tTimer = Timer Call GetCursorPos(tLastPos) End If 'Check for Activity Call GetCursorPos(tCurPos) If tCurPos.x <> tLastPos.x Or tCurPos.y <> tLastPos.y Then 'Mouse Movement tTimer = Timer Else For iKey = 1 To 255 If GetAsyncKeyState(iKey) Then Exit For Next If iKey > 255 Then 'No Activity, Idling Caption = "Idle Time: " & Int(Timer - tTimer) & " Seconds." Else 'Key/Mouse Button Pressed tTimer = Timer End If End If tLastPos.x = tCurPos.x tLastPos.y = tCurPos.y Timer1.Enabled = True End Sub