473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

PreFilterMessage

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.
Mar 19 '07 #1
9 4584
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.

"tclancey" <tu**@idcodeware.co.ukwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
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.

Mar 19 '07 #2
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.

On Mar 19, 1:58 pm, "tclancey" <t...@idcodeware.co.ukwrote:
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.

Mar 20 '07 #3
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.

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@e1g2000hsg.googlegro ups.com...
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.

On Mar 19, 1:58 pm, "tclancey" <t...@idcodeware.co.ukwrote:
>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.


Mar 21 '07 #4
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.

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@e1g2000hsg.googlegro ups.com...
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.

On Mar 19, 1:58 pm, "tclancey" <t...@idcodeware.co.ukwrote:
>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.


Mar 21 '07 #5
On Mar 21, 12:35 pm, "tclancey" <t...@idcodeware.co.ukwrote:
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.

Mar 21 '07 #6
On Mar 21, 12:01 pm, "tclancey" <t...@idcodeware.co.ukwrote:
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.

Mar 21 '07 #7
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.
"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 21, 12:35 pm, "tclancey" <t...@idcodeware.co.ukwrote:
>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.

Mar 21 '07 #8
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" <br*********@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 21, 12:35 pm, "tclancey" <t...@idcodeware.co.ukwrote:
>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.

Mar 21 '07 #9

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 21, 12:35 pm, "tclancey" <t...@idcodeware.co.ukwrote:
>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.

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
Mar 24 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: supster | last post by:
Hello, I'm trying to capture when users press the tab key. This is scrolling through different fields with tab is broken in my form. I'm not sure why, but it may have something to do with the fact...
0
by: Serj Kondryukov | last post by:
I need my program do not see Alt key. That is if user press in textbox Alt+F F must be entered to textbox. I have try to write PreFilterMessage where i hide Alt from LParam and replace...
0
by: terryrey | last post by:
Hi, I've got a winform control that is using Twain and which works fine in a windows application. I am now trying to get this hosted and running in IE6 for our intranet app but am running into...
3
by: feng | last post by:
In our VB.Net Windows application we need to implement a "timeout" type of funtionality - If the the user leave the application idle for certain peroid of time, depending on configureable setting,...
1
by: Neil Stevens | last post by:
Hi, I am in the process of developing a popup utility for a project i am working on, the basic idea is that when a user presses the control key a popup menu will be displayed listing the shortcut...
4
by: GrandpaB | last post by:
Hi, I recently had a post about how to block Mousewheel events. The answer was to implement an IMessageFilter. Sadly, I must report that after 24 hours of researching my library and online...
5
by: Frank Rizzo | last post by:
I have a Combo Box, with DropDownStyle=DropDownList. I have a requirement that a user not be able to click certain items in the dropdown, e.g. the dropped part of the combo box should act like...
0
by: ah.ping.luk | last post by:
Dear All, i want to catch the application event (WM_CLOSE) in a user control. i create a class that implement the IMessageFIlter interface as follow: ...
0
by: jhop | last post by:
Hello everyone, I am having a slight problem with a program i am working on for a class, I am using Visual Studio 2005. The program only allows the up arrow key as input. In the code it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.