Private Type PointApi X As Long Y As Long End TypePrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointApi) As Long Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long Private Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Up_Time, Old_Point As PointApiPublic Function FreeTime() As Long If GetForegroundWindow = Application.hWndAccessApp Then '判斷Mouse是否在使用 Dim New_Point As PointApi GetCursorPos New_Point If Old_Point.X <> New_Point.X Or Old_Point.Y <> New_Point.Y Then Up_Time = Time Old_Point = New_Point Exit Function End If '判斷鍵盤是否在使用 Dim KeyStat(0 To 255) As Byte Call GetKeyboardState(KeyStat(0)) For I = 0 To 255 If (KeyStat(I) And &H80) = &H80 Then Up_Time = Time Exit Function End If Next End If Ext: FreeTime = DateDiff("s", Up_Time, Time) End FunctionPrivate Sub UserControl_Initialize() Up_Time = Time End Sub
常见的用TIMER检测吧
Private Type PointApi
X As Long
Y As Long
End TypePrivate Declare Function GetCursorPos Lib "user32" (lpPoint As PointApi) As Long
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As LongPrivate Up_Time, Old_Point As PointApiPublic Function FreeTime() As Long
If GetForegroundWindow = Application.hWndAccessApp Then
'判斷Mouse是否在使用
Dim New_Point As PointApi
GetCursorPos New_Point
If Old_Point.X <> New_Point.X Or Old_Point.Y <> New_Point.Y Then
Up_Time = Time
Old_Point = New_Point
Exit Function
End If
'判斷鍵盤是否在使用
Dim KeyStat(0 To 255) As Byte
Call GetKeyboardState(KeyStat(0))
For I = 0 To 255
If (KeyStat(I) And &H80) = &H80 Then
Up_Time = Time
Exit Function
End If
Next
End If
Ext:
FreeTime = DateDiff("s", Up_Time, Time)
End FunctionPrivate Sub UserControl_Initialize()
Up_Time = Time
End Sub
但需要添加TIMER否则程序只能
执行一次!