• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Visual Basic Timing and Idle Question

markjrubin

Golden Member
Jan 17, 2000
1,419
0
0
Hi. I've already written an app that solves the problem below, but I'm looking to enhance the app. After the description of the original problem, I've included a new problem
Our website offers Live Customer Service via chat. If there are no operators available, the link to the live chat shouldn't be there. I wrote a program that resides in the system tray that allows operators to login to a MySQL database on our server and update whether or not they're available. If they are available, then the link is displayed. When they close the app, or choose to logout, the link is no longer displayed.

Here's my latest problem. Our operators are forgetting to logout, so the link stays up. Now I need to find a way to determine if there has been no system activity (Instant Messenger has a perfect implementation of this), to automatically log the operator off (so there is no link to the Live Chat) and tell the operator they've been logged out.

How would I get started on this? I've loaned out all of my books, so I don't have any books here, so sample code would be extremely appreciated.

Mark
 

kmmatney

Diamond Member
Jun 19, 2000
4,363
1
81
I can send you a VB routine for checking whether or not the system is idle (and for how long).

Here a cheap way of doing it. The following function will report if the mouse is moved or any key was pressed. I have a better way in a zip file.


Option Explicit


Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Public Type POINTAPI
X As Long
Y As Long
End Type


Public Function CheckIdleState()As String
Dim kKey As Integer 'Stores Each Key on the keyboard in the For Next Loop
Dim CurrentMousePos As POINTAPI 'Used To store the current mouse position
Static OldMousePos As POINTAPI 'Static-keeps the old mouse position
Static IdleTime As Date 'Stores the time In a date variable
Dim SystemIdle As Boolean'Stores weather the systme is idle or Not
SystemIdle = True 'Sets the idle value To true


For kKey = 1 To 256 'steps through Each key on the keyboard it detect If


If GetAsyncKeyState(kKey) <> 0 Then 'any of the keys have been pressed
Debug.Print &quot;Key Pressed&quot;
SystemIdle = False 'Sets the idle value To false
Exit For 'Exits the for Next Loop so that it will move on To the next step
End If
Next
GetCursorPos CurrentMousePos 'Gets the current cursor position and stores it
If CurrentMousePos.X <> OldMousePos.X Or _
CurrentMousePos.Y <> OldMousePos.Y Then 'Checks To see If the cursor has moved
Debug.Print &quot;Mouse Moved&quot;
SystemIdle = False'since the last time it was checked
End If
OldMousePos = CurrentMousePos 'Stores the current mouse position For comparring positons the
'next time through


If SystemIdle = True Then 'If a key hasn't been pressed and the mouse hasn't moved


If DateDiff(&quot;s&quot;, IdleTime, Now) >= 60 Then 'it sets the return value To the elapsed time value
IdleTime = Now 'Resets the time To check the Next minute For idle
CheckIdleStaate = CheckIdleState + 1 'sets the return value In minutes of being idle
End If
Else
IdleTime = Now 'Sets the new Current Idle Time To check For elapsed time
End If
End Function