VB icon

Activity/Inactivity Monitor for Application or Global

Email
Submitted on: 2/12/2015 9:16:00 AM
By: Parmender Dahiya (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 530
 
     How to get the inactivity timeout for an application or globally? Here is the answer. Application timeout means a particular application is running but no mouse or keyboard activity is there for that application for a particluar time. On PSC I found only for Global but this code has both application and global.I have made for the application also. This code can be directly incorporated into your application in minutes.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Activity/Inactivity Monitor for Application or Global
'**************************************
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Activity/Inactivity Monitor for Application or Global
' Description:How to get the inactivity timeout for an application or globally? Here is the answer. Application timeout means a particular application is running but no mouse or keyboard activity is there for that application for a particluar time. On PSC I found only for Global but this code has both application and global.I have made for the application also. This code can be directly incorporated into your application in minutes.
' By: Parmender Dahiya (from psc cd)
'**************************************

'************************************************************
'HOW TO USE THIS CODE
' 1. ADD a Timer Control with name ActivityTimer on form with 100 millisecond interval
' 2. ADD a Label with name label1
' 3. COPY and PASTE this code into your code in the form where the above controls are added
'************************************************************
Option Explicit
' API Definitions
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
 x As Long
 y As Long
End Type
Dim GlobalTimeout As Boolean
' this is the time you want to set for inactivity
' if you want to define 60 seconds then TimeOutTiem constant must be
' multiplied by 10 because timer control on form is 100 millisecond
Private Const TimeoutTime = 50
Private Sub ActivityTimer_Timer()
Dim KeyPressed As Boolean, MouseMoved As Boolean, tmpPos As POINTAPI
Dim i As Integer, Ret As Long
Static MyCount As Long, OtherCount As Long
Static MousePos As POINTAPI
KeyPressed = False
MouseMoved = False
GlobalTimeout = False
' find if any of the keyboard is pressed
' if a key is pressed on keyboard then value returned by
' GetAsyncKeyStatewill be -32767
For i = 0 To 255
 If GetAsyncKeyState(i) = -32767 Then
KeyPressed = True
Exit For
 End If
Next
' get the mouse position
Ret = GetCursorPos(tmpPos)
' compare with previous mouse positions
If tmpPos.x <> MousePos.x Or tmpPos.y <> MousePos.y Then
 MouseMoved = True
 MousePos.x = tmpPos.x
 MousePos.y = tmpPos.y
End If
' if NO keyboard or mouse activity then increment the count
' if activity then two things two be checked
' 1. If it is on any other application or on our application
' 2. If we have to check for Global or Local (Variable GlobalTimeout)
If KeyPressed = True Or MouseMoved = True Then
 If GlobalTimeout = False Then ' if checked for our application
If GetForegroundWindow() = Me.hwnd Then
 MyCount = 0
Else
 MyCount = MyCount + 1
End If
 Else' if checked globally
MyCount = 0
 End If
Else' if no keyboard or mouse activity
 MyCount = MyCount + 1
End If
Label1.Caption = MyCount
' if count has exceeded the limit
If MyCount >= TimeoutTime Then
 MsgBox "Timeout Occurred after " & MyCount / 10 & " seconds", vbOKOnly + vbCritical, "No Avtivity"
 End' TAKE ACTION HERE AFTER THE TIMEOUT
End If
End Sub


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.