How to monitor mouse and keyboard actions?

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
I mentioned in another thread that I lock my app after a certain period of disuse. The only problem is I don't know how to get whether the mouse has been moved or a key has been pressed. I know that there is supposed to be a way to do this through the API but I have absolutely no idea how to use the API, let alone manipulate it to do what I want!

I think that you use the API to monitor the event queue and then any event which is mouse event or keyboard event related to the app then you restart the idle timer for locking the app. But I really don't know how to go about this. If so someone could offer some advice or help it would be very much appreciated.
 
Fear not, you don't need to resort to messy API for this one. The framework provides an excellent way of checking your applications message loop. You need to create a class that implements the IMessageFilter Interface. It has one method called 'PreFilterMessage' in there you can check for any messages are in your applications message loop and choose to act on them or even filter them out completely. All you need to do to activate your message filter class is pass it to the Application classes AddMessageFilter method.


Application.AddMessageFilter(the class that implements the message filter)

You can then alos remove it when you are done useing the removemessagefilter method.

Check it out, i'm sure it will be able to help you to do want you need.
 
Last edited:
I you need any help with this one uncleRonin dont hesitate to give me a shout. The only thing that you will need to 'get a hold' of the messages are the hexdecimal values that represent the messages you want to intercept.
 
*blink* uhh...... I knew that! *falls over* Okay, so I implement the class (I'll add this class to my project for simplicity's sake). Then in my main Load or Show or wherever I add the messagefilter to the app (I must instantiate the class first though right?). This adds the filter okay?

Then inside the filter class, I catch any mouse or keyboard events which are triggered, through the PreFilterMessage method. Now, you say I have to use Hex? How can I tell which messages are which? Is there some source where I can get the needed Hex values? *scratch* So, in the PFM I simply case the mesages and then execute whatever code needs to be executed.

I still haven't had a chance to do this yet (gotta get the main functionality right first).

If you don't mind me asking: how on earth did you find out about this? It's so much simpler than every other method I've seen!

This message queue, is it for the application only? So, any activity that happens within the application will send a message which will be filtered through PFM before being passed to whatever needs it? Will these messages always have a control that is being referenced or will it include things like checking variables and other stuff like that?
 
First question - Yes you need an object reference to the class that implements the Interface. But you can do it all in the sub new of that class..

VB.NET:
'Just for example
 
Public class MessageFilter
Implements ImessageFilter
 
Public sub new
Application.AddmessageFilter(me)
end sub

Second Question - DownLoad a copy of the API VIEWER you can google for it. In there is every Hexdecimal constant you will ever need. The Hexdecimal values are the messages that are sent in your application message loop. In the arguments of the sub it will say

VB.NET:
ByRef m as message

Where m.msg is the value you want to check for against the hexdecimal constant value of the message you want to intercept. With me?

Third question : cant remember how i found out about this, a bit of luck whilst googling i suspect.


Forth Question: Yes thats the beauty of this interface and the way messages work in windows. Every Application has it's own separate message loop so don't worry you can't do anything to any other running application from there. The message will contain a handle to the window it's being sent to (EVERYTHING in windows is a window, buttons, texboxes, comboboxes, forms, everything) Also you will find a Wparam and a lParam for now i would'nt worry too much about these, but needless to say they contain extra information.
 
You could also use a timer control, and monitor (say every 30 secs) the name of the currently selected control.

If this control has changed, then your counter resets, but if your counter reaches 300, then it means your app has not been used for 5 minutes.

This has another advantage in that the user can use other programs, but your program will still lock up if it is not being used.
 
Back
Top