PreFilterMessage

  • Thread starter Thread starter tclancey
  • Start date Start date
T

tclancey

Thanks for the information provided so far for this problem, I don't seem to
be able to implement this as I would like to. I'm probably doing something
particularly stupid!

I've added a PreFilterMessage Sub to the Application Events, I want to get
any keyboard or mouse click throughout the entire application, when I get
one I want to start a timer.

All the examples I can find on the web say I should add
Application.AddMessageFilter(Me) to the main form load event.

When I do this I get a run time error:
Unable to cast object of type 'MyApp.Form1' to type
'System.Windows.Forms.IMessageFilter'.

Can anyone show me the error of my ways? !

Cheers,
Tull.
 
Well, I've got somewhere, I don't get an error any more. Here is the sample
Function:

Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As
Boolean Implements IMessageFilter.PreFilterMessage
Dim sw As New StreamWriter("c:\log.txt", True)
sw.WriteLine(Now & " " & m.Msg)
sw.Close()
sw = Nothing
End Function

Nothing ends up in the file. I'm obviously just not understanding how this
works! I don't really want a function, I'd sooner have a sub.

Any help greatfully recevied.
Cheers,
Tull.
 
Have you instantiated the class containing this method and passed it
as a parameter to Application.AddMessageFilter?

If you're still having problems post a short, but complete program
demonstrating the problem you're having.
 
Difficult to do as the application is large, very.

Would it be possible for you to explain the steps to get this working?
Cheers,
Tull.
 
Ah! I understand.

All I was missing was to get mybase to add the filter.

My log file now holds loads of crap!

21/03/2007 17:32:16 1025
21/03/2007 17:32:16 49389
21/03/2007 17:32:16 49389
21/03/2007 17:32:16 49396
21/03/2007 17:32:16 49621
21/03/2007 17:32:16 512
21/03/2007 17:32:16 15
21/03/2007 17:32:16 15
21/03/2007 17:32:16 15

etc.

Is there a list anywhere of what all the codes are? All I want to do is
'know' if the keyboard or mouse has been used, I don't actually want to act
on any specific event.

Cheers,
Tull.
 
Ah! I understand.

All I was missing was to get mybase to add the filter.

My log file now holds loads of crap!

21/03/2007 17:32:16 1025
21/03/2007 17:32:16 49389
21/03/2007 17:32:16 49389
21/03/2007 17:32:16 49396
21/03/2007 17:32:16 49621
21/03/2007 17:32:16 512
21/03/2007 17:32:16 15
21/03/2007 17:32:16 15
21/03/2007 17:32:16 15

etc.

Is there a list anywhere of what all the codes are? All I want to do is
'know' if the keyboard or mouse has been used, I don't actually want to act
on any specific event.

Cheers,
Tull.

Yep, those messages are going to come in fast and furious. That's why
there's usually a condition that checks for one or two specific
messages. The codes are probably defined in a series of C++ header
files (*.h) in #define statements. The messages usually begin with
WM_. Try googling for WM_KEYDOWN and WM_CLICK.
 
Difficult to do as the application is large, very.

Would it be possible for you to explain the steps to get this working?
Cheers,
Tull.

I see that you have it figured out so it's moot at this point, but
what I meant is for you to create a new project from scratch and try
to get it working in the most trivial case. A lot of times you'll
figure out the problem yourself and if not then all you have to do is
post that short program for us to look at.
 
I realise what you said about creating a new application, usually I would
test anything new in a test environment, but on this occation I was being
stubborn! I know what I was doing was correct and I was missing something
silly, and indeed I was!

Yes, I realised that every single application event would come through here
so I was expecting a lot of data, in fact I was expecting more than I got.
I think I'm only getting events from the main form, I need to trap all
events in the complete application. Do I simply move the handler to the
Application level? I think I've tried this and it didn't like the idea!

Any ideas?
Cheers,
Tull.
 
Ignore the last message, I am getting message from all forms. Looking at
the numbers and times I can take an educated guess as to which are keyboard
and which are mouse, but obviously guessing isn't the best practice!

If anyone knows where I can get a list of the message number meanings I
would be very greatful. I don't really want to have to search through loads
of constants if I can help it. I basically need to know if the mouse has
been clicked, or a key has been pressed. That's it.

Can anyone give me a rought idea of overhead of this function? Obviously
while I'm writing every message to disk I'm seeing a degradation of
performance, but not what I would call a great deal. This surprised my as
opening a file, writing, then closing is a hefty job.

Also, is there any way of implementing this as a Sub rather than a Function
so the app doesn't wait for the False Return? It's just not nessesary to me
in this instance.

Presumably this is the way people write keyboard loggers and the like, very
easy, scary stuff!

Cheers,
Tull.
 
Brian Gideon said:
Yep, those messages are going to come in fast and furious. That's why
there's usually a condition that checks for one or two specific
messages. The codes are probably defined in a series of C++ header
files (*.h) in #define statements. The messages usually begin with
WM_. Try googling for WM_KEYDOWN and WM_CLICK.


WinUser.h is a lot of help for these, but once when I was investigating an
intermittant screen redraw problem I had to watch these events.. Lot of
fun.
I do remember being pleasantly surprised to find out that Message.ToString
gives useful english names. It has been a while, but poke around the
objects
properties, and you will find this.

HTH
 
Back
Top