Results 1 to 2 of 2

Thread: Checking for user activity.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    8
    Im trying to find some information on getting user activity/inactivity via a timer.

    I have an MDI form with a number of child forms I want to trap for key activity or mouse activity so that no matter what form I am in if time > x then I can execute a function, much the same way as a screensaver works in windows i would guess.

    I have thought about having a timer class that when program loads it starts timer then in each form performing a reset on timer value if key or mouse is used but this isnt viable as I need to hard code each form seperatley.

    If anyone has any ideas or can point me to somewhere that has a similar example I'd be grateful.

    Thnx

  2. #2
    Guest
    U can use the GetCursorPos API to check the mouse position, store it and compare the previous position with he current position.

    And the GetASyncKeyState to see if any key has been pressed since you last checked it(the function does this automaticaly).

    Here is a simple example (add a timer to a form and set its interval to 1000, it counts the seconds that there hasn't been any activity)

    Code:
    Option Explicit
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Sub Timer1_Timer()
        Static lNotActive As Long
        Static LastCursorPos As POINTAPI
        Dim CursorPos As POINTAPI
        Dim lCounter As Long
        
        lNotActive = lNotActive + 1
        GetCursorPos CursorPos
        If (CursorPos.x <> LastCursorPos.x) Or (CursorPos.y <> LastCursorPos.y) Then
            LastCursorPos = CursorPos
            lNotActive = 0
        Else
            For lCounter = 0 To 255
                If GetAsyncKeyState(lCounter) <> 0 Then
                    lNotActive = 0
                End If
            Next
        End If
        
        Caption = lNotActive
    End Sub
    Hope it helps

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