Results 1 to 3 of 3

Thread: System idle time

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    2

    Post

    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]

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    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]


  3. #3
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    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 "&lt;&gt;" with "<>" and "&gt;" with ">".

    Just an FYI.

    Here it is, edited:
    VB Code:
    1. Private Type POINTAPI
    2.         x As Long
    3.         y As Long
    4. End Type
    5.  
    6. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    7. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    8.  
    9. Private Sub Form_Load()
    10.     Timer1.Interval = 100
    11.     Timer1.Enabled = True
    12. End Sub
    13.  
    14. Private Sub Timer1_Timer()
    15.     Static tTimer As Single
    16.     Static tLastPos As POINTAPI
    17.     Dim tCurPos As POINTAPI
    18.     Dim iKey As Integer
    19.    
    20.     Timer1.Enabled = False
    21.     If tLastPos.x = 0 And tLastPos.y = 0 Then
    22.         'First Time, Setup the Variables
    23.         tTimer = Timer
    24.         Call GetCursorPos(tLastPos)
    25.     End If
    26.     'Check for Activity
    27.     Call GetCursorPos(tCurPos)
    28.     If tCurPos.x <> tLastPos.x Or tCurPos.y <> tLastPos.y Then
    29.         'Mouse Movement
    30.         tTimer = Timer
    31.     Else
    32.         For iKey = 1 To 255
    33.             If GetAsyncKeyState(iKey) Then Exit For
    34.         Next
    35.         If iKey > 255 Then
    36.             'No Activity, Idling
    37.             Caption = "Idle Time: " & Int(Timer - tTimer) & " Seconds."
    38.         Else
    39.             'Key/Mouse Button Pressed
    40.             tTimer = Timer
    41.         End If
    42.     End If
    43.     tLastPos.x = tCurPos.x
    44.     tLastPos.y = tCurPos.y
    45.     Timer1.Enabled = True
    46. End Sub
    Last edited by VictorB212; Jul 12th, 2001 at 11:35 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width