Link to home
Start Free TrialLog in
Avatar of chekhov
chekhov

asked on

Monitoring keyboard activity

I need to monitor keyboard and mouse activity, and call a function when the mouse or keyboard have not been used for the last 5 minuts, similar to what a screensaver does. How can I do this with VB4?
Avatar of rantanen
rantanen

Hi,

here's a simple example. Put following code in a form. On this form place a textbox, a picturebox and a command button keeping their default names.

Option Explicit
Private LastX As Single ' These will change if there is a mouse
Private LastY As Single ' or keyboard activity on the form.
Private nStart As Integer ' Inativity period in seconds


' For every controls MouseMove event place following two lines
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    LastX = X
    LastY = Y
End Sub

' Form processes first key presses emulating mouse movement
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Static nCount As Integer
   
    LastX = nCount
    LastY = nCount
    nCount = nCount + 1
    If nCount = 1000 Then nCount = 0
End Sub


Private Sub Form_Load()
    Form1.KeyPreview = True ' We are checking if any keypresses take place
    nStart = 20 ' seconds '(Your value comes here)
    Timer1.Interval = 1000 ' milliseconds (we check once a second)
    Timer1.Enabled = True
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    LastX = X
    LastY = Y
End Sub


Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    LastX = X
    LastY = Y
End Sub


Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    LastX = X
    LastY = Y
End Sub


Private Sub Timer1_Timer()
    Static nCountDown
   
    If AnyActivity Then
        nCountDown = nStart
    Else
        nCountDown = nCountDown - 1
    End If
    If nCountDown = 0 Then
        MsgBox "You haven't moved your mouse for " & Str(nStart) & " seconds!"
    End If
    Text1 = nCountDown
End Sub



Private Function AnyActivity() As Boolean
    Static MyLastX As Single
    Static MyLastY As Single

    If (MyLastX <> LastX) Or (MyLastY <> LastY) Then
        AnyActivity = True
    Else
        AnyActivity = False
    End If
    MyLastX = LastX
    MyLastY = LastY
End Function

If any questions, make a comment. And...yes...this simple example does not respond to TAB and ENTER keys, it is another story.
Avatar of chekhov

ASKER

I'm afraid I didn't state the question correctly. I need to monitor when keyboard and mouse activity hasn't occured for ANY application, that is, if the user hasn't touched the keyboard or mouse for a certain period of time, just like a screensaver does. Your example only monitors activity for this application.

Also the VB Timer control, that you suggest using, takes too much resources, I need something that won't slow down the machine, again, similar to what a screensaver does.
Use the SetWindowHook API call to set a system wide hook for both keyboard message and mouse message.

That mean that all messages Windows send to all applications that are about the mouse or the keyboard will pass through your hook procedure first. It is thoses message that finally triger Vb's event's.
Just monitor their passage and kick on when it's been a  minute you didn't see any.

You will need Vb5 to set up the hook. Previous version of Vb do not have the AddressOf operator that is needed to tell Windows which is (and where is) your procedure that monitor messages.
Hi Chekhov,
           I have a "FREEWARE" DLL which does exactly
           what you want . Comes with free sample too.
           Works in VB3 and VB4 . Give me your e-mail
           address . I can send it you . If you are interested.
Avatar of chekhov

ASKER

To Imarceau:
Thanks for the answer, but could you be a little bit
more specific, I don't have too much experience using the
API. If you could just sketch out the things that the code should do, please.

To andaluri:
You say you have a DLL that does that, sure I would like to try
it, my address is tonydi@usa.net  Although I would prefer the
source code, I think I can write it if Imarceau or somebody else
can sketch out what the code should do.


OK, I misunderstood you.  But I like to make couple of comments.

First, when you say "..similar to what a screensaver does" gives a wrong clue. It is not the screen saver which monitors the activity on input devices systemwide, it is Windows itself.

Another thing is that after screensaver is kicked awake, it starts monitoring those activities grabbing control of the whole system (usually launching a system modal window) That is what screen saver does (only I thought you want to monitor only one app).

And how do you measure time without a timer? And with a timer I don't mean only timer controls, it can also be a piece of code constantly looping. And usually screen savers make heavy use of timers because of the animation involved.

Finally, a stupid question: I don't know what your function will do, but can't you put it into a minimal screen saver? Then Windows will do the monitoring for you.
Avatar of chekhov

ASKER

Yes, I will need a timer, constantly looping, but the VB Timer control takes too many resources, I'm sure there's an API function that can keep track of time, and is more efficient.

The purpose of this function is to make routine maintenance opeations, like backups, and I want to do them when the machine is turned on but not in use. If the machine hasn't been touched in say, half an hour, then it's probably not being used.
OK,

I see two possibilities here now.

First, Plus! package for Win 95, it contains SAGE (System Agent) which you can use to schedule any program to run.

Another is that you write a "screen saver" which does what you want.

I personally prefer SAGE:
You probably need a system-wide hook. Check out the following page for VB5 code which deals with an application hook. It should be pretty easy to generalise to a system hook if you read the win32 docs.

http://www.beadsandbaubles.com/coolvb/subclass.shtml
ASKER CERTIFIED SOLUTION
Avatar of andaluri
andaluri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dr. Kamal Mehdi
andaluri

Can you send me this zip file please?
My e-mail is: kamal@egnatia.ee.auth.gr

Thank you very much.
Kamal