Use the imessagefilter interface to intercept messages from the keyboard or mouse

Source: Internet
Author: User
In C #, you can use the imessagefilter interface to intercept the message I messagefilter interface of the application, allowing the application to capture the message before it is scheduled to the control or form. You can add classes that implement the imessagefilter interface to the Message Pump of an application to filter messages or perform other operations before they are scheduled to the control or form. To add the message filter to the Message pump of the application, use the addmessagefilter method in the application class.
The application. addmessagefilter method adds a message filter to monitor Windows messages sent to the target.
[Visual Basic] public shared sub addmessagefilter (byval value as imessagefilter)
Parameter Value: the implementation of the imessagefilter interface to be installed.
Note: Use message filters to prevent a specific event from being triggered, or use message filters to perform special operations on an event before it is passed to the event handler. The message filter is unique to a specific thread.
To prevent messages from being scheduled, the value parameter instance passed to the method must overwrite the prefiltermessage method with the code that processes the message. This method must return false.

Warning adding a message filter to the Message Pump of an application reduces performance.

The following is an example of how to use the imessagefilter interface to intercept messages from the keyboard or mouse:

Internal class mymessager: imessagefilter
{
Public bool prefiltermessage (ref message m)
{
// If a message with a mouse or a keyboard is detected, the count is 0 .....
If (M. MSG = 0x0200 | M. MSG = 0x0216 | M. MSG = 0x0201 | M. MSG = 0x0204 | M. MSG = 0x0207)
{
Winedetail. iopercount = 0;
}
Return false;
}
}

Add the following code to the constructor of the screen to capture the form:

// Intercept messages sent by the program to the System
Mymessager MSG = new mymessager ();
Application. addmessagefilter (MSG );

Set a timer on the screen to identify and execute events in the timer method:

Private void timereffectick (Object sender, eventargs E)
{
Iopercount ++;
If (iopercount> 10)
{

// Do something ....

}

}

The above is the logic of a simple demo program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.