【发布时间】:2014-04-01 19:08:07
【问题描述】:
我正在尝试构建一个程序来计算自上次单击鼠标以来的时间(并在给定的不活动时间间隔后执行一些操作)。不幸的是,鼠标单击事件似乎不断触发。 txtLastMouseMove 不断更新以每秒显示当前时间,而 txtTimeSinceMouseMove 永远不会超过 1,通常为 0。
Timer1 设置为 100 毫秒。将其设置为更长的时间间隔会减慢更新速度,但它们仍然无法正确计数。
我在这里缺少什么?为什么鼠标左键事件不断发生?
' 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
【问题讨论】: