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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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
        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
        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.
chekhovAuthor Commented:
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.
chekhovAuthor Commented:
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  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.
chekhovAuthor Commented:
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.

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.
Hi Chekhov,
            I just sent a zip file to you.
            Trust me it works! . Save yourself some time
            and some headache too . All the information you
            need is in the zip file . It's free .
            Have a nice day!.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dr. Kamal MehdiCommented:

Can you send me this zip file please?
My e-mail is:

Thank you very much.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.