Reputation: 133
I'm trying to build a program that counts how long it's been since the mouse was last clicked (and performs some action after a given interval of inactivity). Unfortunately, the mouse click event seems to fire constantly. txtLastMouseMove continually updates to show the current time every second, and txtTimeSinceMouseMove never gets above 1, and is usually 0.
Timer1 is set to 100 ms. Setting it to a longer interval slows down the updates, but they still never count properly.
What am I missing here? Why does the left-click mouse event happen continuously?
' Detect mouse clicks.
Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Long) As Integer
Private Type POINTAPI
x As Long
y As Long
End Type
' Detect mouse clicks.
Private Sub Timer1_Timer()
Dim Ret As Integer
Static datLastMouseMove As Date
Dim datThisMouseMove As Date
if (datLastMouseMove=0) then datLastMouseMove=now()
Ret = GetAsyncKeyState(1) 'vbKeyLButton = 1
If Ret < 1 Then
' The left mouse button was clicked.
datThisMouseMove = Now()
txtLastMouseMove.Text = Format(datThisMouseMove, "hh:mm:ss")
txtTimeSinceMouseMove = Format(datThisMouseMove - datLastMouseMove, "hh:mm:ss")
If ((datThisMouseMove - datLastMouseMove) * 24 * 60 * 60 > CDbl(txtInterval)) Then
MsgBox "Mouse has not moved in " & Format(datThisMouseMove - datLastMouseMove, "hh:mm:ss")
End If
datLastMouseMove = datThisMouseMove
End If
End Sub
Upvotes: 0
Views: 232
Reputation: 133
Nevermind, I'm stupid.
"If Ret < 1 Then" fires anytime the mouse button ISN'T clicked. It should read "If Ret Then".
Upvotes: 1