Solved

Monitoring keyboard activity

Posted on 1997-11-13
11
453 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 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!.


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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now