Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Monitoring keyboard activity

Posted on 1997-11-13
11
Medium Priority
?
475 Views
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?
0
Comment
Question by:chekhov
[X]
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
11 Comments
 
LVL 4

Expert Comment

by:rantanen
ID: 1440753
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.
0
 

Author Comment

by:chekhov
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.
0
 
LVL 1

Expert Comment

by:lmarceau
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:andaluri
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.
0
 

Author Comment

by:chekhov
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 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.


0
 
LVL 4

Expert Comment

by:rantanen
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.
0
 

Author Comment

by:chekhov
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.
0
 
LVL 4

Expert Comment

by:rantanen
ID: 1440760
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:
0
 
LVL 3

Expert Comment

by:wolfcrag
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.

http://www.beadsandbaubles.com/coolvb/subclass.shtml
0
 
LVL 1

Accepted Solution

by:
andaluri earned 400 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!.


0
 
LVL 7

Expert Comment

by:kamall
ID: 1440763
andaluri

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

Thank you very much.
Kamal

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

721 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