Intercept keyboard message with iMessageFilter interface Sunhai
IMessageFilter interface
The iMessageFilter interface allows applications to capture it before messages are scheduled to controls or forms.
You can add a class that implements the iMessageFilter interface to the application's message pump to place it before the message is scheduled to the control or form.
It screens or performs other operations. To add message filters to the application's message pump, use the Application class.
AddMessageFilter method.
Application.AddMessageFilter method
Add a message filter to monitor these messages when transferring Windows messages to the target. [Visual Basic] Public Shared Sub AddMessageFilter (Byval Value As IMESSAGEFILTER)
Parameter Value The implementation of the iMessageFilter interface you want to install. Remarks uses a message filter to prevent specific events, or perform special events before using a message to pass an event to the event handler.
Action. The message filter is unique to a particular thread. To prevent scheduling a message, the value parameter instance that passes to the method must be rewritten using the code for the message to rewrite the prefilterMessage method.
. This method must return false. WARNING Add a message filter to your application to reduce performance. Example [Visual Basic, C #, C ] must first provide an implementation of the iMessageFilter interface before you can use a message filter.
The following class creates a message filter called TestMessageFilter. This filter prevents all messages related to the left mouse. [Visual Basic] 'Creates A Message Filter.public Class TestMessageFilter Implements iMessageFilter
Public Function PreFilterMessage (ByRef m As System.Windows.Forms.Message) _ As Boolean Implements IMessageFilter.PreFilterMessage 'Blocks all the messages relating to the left mouse button. If ((m.Msg> = 513) And (m.Msg < = 515)) THEN CONSOLE.WRITELINE ("Processing The Messages:" & m.msg) Return True End IF Return False End Functionend Class
Use an ESC key to exit the program
Implements IMESSAGEFILTER
Private Sub Form1_Load (Byval Sender As System.Object, Byval E AS System.EventArgs) Handles
MyBase.Load 'Assign Message to App And Form Application.addMessageFilter (ME) End Sub
Public Function PrefilterMessage (byref m as system.windows.forms.Message) as boolean
Implements iMessagefilter.prefilterMessage Dim Keycode As Keys = CTYPE (M.WParam.Toint32 (), Keys) and keys.keycode if keycode = keys.escape the end 'endpress. We can call various methods here. END IF End Function May 16, 2004
My QQ: 26624998 My website: http://blog.9cbs.net/2066/