VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Detect Idle Mouse (GetCursorPos)

by Richard Puckett (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

This code can be used to determine the X,Y coordinates of the mouse cursor
and use them to check for idle mouse activity. This code is useful in that it does
not require your current form to be in focus (active windows status). The
GetCursorPos can be used in conjunction with or be replaced by another API
call GetCaretPos, which determines the X,Y coordinates of the text cursor.
Hopefully this will be useful to anyone looking to check for an idle desktop.
(Richard Puckett, [email protected])

Assumes
1. Take out all of the ME.PRINT statements, since they are only to illustrate how the function works (I used this code in a login program to monitor mapped network drive connections in lab environments.) 2. All of the declarations are in a module. 3. This example is only using the X coords to determine activity, I am sure a more complex method can be devised.
API Declarations
'API Call establishes mouse coords
Public Declare Function GetCursorPos Lib "user32" _
(lpPoint As PointAPI) As Long
Public Pnt As PointAPI
'These values MUST be public
Public OldX As Long
Public OldY As Long
Public NewX As Long
Public NewY As Long
Public Type PointAPI
X As Long
Y As Long
End Type
'This Const determines the total timeout value in minutes
Global Const MINUTES = 15
Public TimeExpired
Public ExpiredMinutes

Rate Detect Idle Mouse (GetCursorPos)


Public Sub Form_Load()
  
  Timer1.Interval = 1000
  
  OldX = 0
  OldY = 0
    
End Sub
Public Sub Timer1_Timer()
             
  GetCursorPos Pnt
           
    Me.Cls
    Me.Print "The current mouse coordinates are "; _
    Pnt.X; ","; Pnt.Y
    
  NewX = Pnt.X
  NewY = Pnt.Y
    
    Me.Print "OldX coords", OldX
    Me.Print "OldY coords", OldY
        
    Me.Print "NewX coords", NewX
    Me.Print "NewY coords", NewY
       
    If OldX - NewX = 0 Then
      Me.Print "No Movement Detected"
      TimeExpired = TimeExpired + Timer1.Interval
      Me.Print "Total Time Expired", TimeExpired
    Else
      Me.Print "Mouse is Moving"
      TimeExpired = 0
    End If
   
  OldX = NewX
  OldY = NewY
    
    ExpiredMinutes = (TimeExpired / 1000) / 60
    
    If ExpiredMinutes >= MINUTES Then
    TimeExpired = 0
    Me.Print "Times Up!!!"
    
    End If
End Sub

Download this snippet    Add to My Saved Code

Detect Idle Mouse (GetCursorPos) Comments

No comments have been posted about Detect Idle Mouse (GetCursorPos). Why not be the first to post a comment about Detect Idle Mouse (GetCursorPos).

Post your comment

Subject:
Message:
0/1000 characters