Results 1 to 4 of 4

Thread: How does Windows know when to call the screensaver?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    13

    Post

    Is there an API-call or something to check how much time passed since the last keypress
    or mouse movement?


  2. #2
    Member
    Join Date
    Jul 1999
    Posts
    40

    Post

    Timer Control

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    13

    Post

    Well, this might work for one single application but not for the whole system. using just a timer control I couldn't access the events of all other apps running, could I? Subclassing Windows isn't possible, too (would be quite nice ;-))!

  4. #4
    Guest

    Post

    Create a new form and add a label and a timer called Label1 and Timer1.

    Use the GetAsyncKeyState to see if a key was pressed since the last time we checked the key.
    And the GetCursorPos to check the last cursor position.

    Code:
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    
    Private Sub Form_Load()
        'Init timer
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim bActive As Boolean
        Dim iCounter As Integer         'Counter to walk throug all key's
        Dim pCursPos As POINTAPI        'Cursor pos
        Static plCursPos As POINTAPI    'Store last cursor posistion
        Static datTime As Date
        
        'Loop through every key
        For iCounter = 1 To 256
            If GetAsyncKeyState(iCounter) <> 0 Then
                'Key was pressed since last time we checked
                bActive = True
                Exit For
            End If
        Next
        
        'Get the cursor pos
        GetCursorPos pCursPos
        
        'Check the cursor pos with the last cursorpos
        If pCursPos.x <> plCursPos.x Or pCursPos.y <> plCursPos.y Then
            bActive = True
        End If
        
        'Store the cursor pos for next time
        plCursPos = pCursPos
        
        'There has been some activity reset the time
        If bActive = True Then
            datTime = Now
        End If
        Label1 = "Inactive for " & DateDiff("s", datTime, Now) & " sec."
    End Sub
    ------------------

    Vincent van den Braken
    EMail: [email protected]
    ICQ: 15440110
    Homepage: http://www.azzmodan.demon.nl




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