Monitoring keyboard activity

Posted on 1997-11-13
Last Modified: 2012-08-14
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?
Question by:chekhov
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3

Expert Comment

ID: 1440753

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.

Author Comment

ID: 1440754
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.

Expert Comment

ID: 1440755
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.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Expert Comment

ID: 1440756
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.

Author Comment

ID: 1440757
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.


Expert Comment

ID: 1440758
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.

Author Comment

ID: 1440759
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.

Expert Comment

ID: 1440760

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:

Expert Comment

ID: 1440761
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.

Accepted Solution

andaluri earned 200 total points
ID: 1440762
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!.


Expert Comment

ID: 1440763

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

Thank you very much.


Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question