Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Article

Using IMessageFilter to create a generic filter for operating system events

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
15 Oct 20031 min read 110.5K   1.5K   39   18
An article on implementing IMessageFilter to create a message filter for operating system messages

Introduction

The IMessageFilter class in .NET framework allows an application to capture a windows OS message before it is dispatched to a control or form. A class that implements the IMessageFilter interface can be added to the application's message pump to filter out a message or perform other operations before the message is dispatched to a form or control. Disabling the keys at the forms level leads to lot of code repetition and something that doesn’t look very elegant. The solution provides a generic solution to the problem by separating the implementation from the UI.

Background

The requirement that lead to this solution was, conditional Enabling/Disabling specified keys on a windows form.

Using the code

On click of button3 the keys passed are disabled and enabled again on click of button4. Although the solution addresses the Key_Pressed event only, it can be extended to filter any operating system event.

C#
private void button3_Click(object sender, System.EventArgs e) 
{ 
  mobjMessageFilter = new MessageFilter(); 
  mobjMessageFilter.KeysToRestrict=new int[]{
    Convert.ToInt32(MessageFilter.VirtualKeys.VK_F2), 
    Convert.ToInt32(MessageFilter.VirtualKeys.VK_F1)}; 
  Application.AddMessageFilter(mobjMessageFilter); 
}
 
private void button4_Click(object sender, System.EventArgs e) 
{ 
  Application.RemoveMessageFilter(mobjMessageFilter); 
} 

The MessageFilter Class

MessgeFilter class implements the IMessageFilter interface and gives the concrete implementation of the Message Filter.

C#
public class MessageFilter:IMessageFilter

The VirtualKeys enumeration

This Enumeration contains virtual key values sent by the operating system when a key is pressed.

C#
public enum VirtualKeys : int 
{ 
VK_LBUTTON = 0x01, 
VK_RBUTTON = 0x02, 
VK_CANCEL = 0x03, 
VK_MBUTTON = 0x04, 
VK_XBUTTON1 = 0x05, 
VK_XBUTTON2 = 0x6, 
VK_BACK = 0x08, 
VK_TAB = 0x09, 
VK_RETURN = 0x0D, 
VK_SHIFT = 0x10, 
VK_CONTROL = 0x11, 
VK_ESCAPE = 0x1B, 
VK_SPACE = 0x20, 
VK_LEFT = 0x25, 
VK_UP = 0x26, 
VK_RIGHT = 0x27, 
VK_DOWN = 0x28, 
VK_DELETE = 0x2E, 
VK_F1 = 0x70, 
VK_F2 = 0x71, 
VK_F3 = 0x72, 
VK_F4 = 0x73, 
VK_F5 = 0x74, 
VK_F6 = 0x75, 
VK_F7 = 0x76, 
VK_F8 = 0x77, 
VK_F9 = 0x78, 
VK_F10 = 0x79, 
VK_F11 = 0x7A, 
VK_F12 = 0x7B, 
VK_F13 = 0x7C, 
VK_F14 = 0x7D, 
}

The WindowsMessages enumeration

This enumeration contains the WindowsMessage value for various Operating System events. Included code contains an exhaustive listing of windows messages.

C#
private enum WindowsMessages: int 
{ 
WM_KEYDOWN = 0x0100 
}

And now to the implementation of the MessageFilter class. It in fact is very simple, all we have to do is give an implementation for the PreFilterMessage method, I have created a property int[] KeysToRestrict to accept the values of the keys to be filtered. And here is what the method looks like,

C#
private enum WindowsMessages: int
{
  WM_KEYDOWN = 0x0100
}

public bool PreFilterMessage(ref Message objMessage)
{
  //Check if the Message is a windows Key down message
  if(objMessage.Msg == Convert.ToInt32(WindowsMessages.WM_KEYDOWN))
  {
    for(int i=0;i<marrKeysToRestrict.Length;i++)
    {
      //Check if the Message's WParam(the key value) is 
in the restricted keys list
      if( objMessage.WParam.ToInt32() == 
marrKeysToRestrict[i])
      //Disable message propagation
      return true;
    }
  }
 return false;
} 

The method returns true if the Message objects WParam property is equal to any element in the array thus disabling the key.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionexcellent! Pin
Southmountain25-May-22 15:04
Southmountain25-May-22 15:04 
QuestionIMessageFilter does not work on Dialog boxes Pin
Ross Brigoli21-Jan-09 17:52
Ross Brigoli21-Jan-09 17:52 
GeneralMesage Filters Pin
RoryHerman18-Sep-08 21:55
RoryHerman18-Sep-08 21:55 
GeneralWonderful Article Pin
anupamaroy14-Dec-05 18:08
anupamaroy14-Dec-05 18:08 
Questionpossible to use with a wedge barcode scanner? Pin
yogiberr22-Nov-05 8:45
yogiberr22-Nov-05 8:45 
AnswerRe: possible to use with a wedge barcode scanner? [modified] Pin
Murpheux Melony25-May-07 6:26
Murpheux Melony25-May-07 6:26 
QuestionDisposing the IMessageFilter? Pin
JediMasterYoder10-Jun-05 11:01
JediMasterYoder10-Jun-05 11:01 
QuestionVB6 Implementation? Pin
TAlvord6-Dec-04 7:32
TAlvord6-Dec-04 7:32 
GeneralThx a lot Pin
Steven Wilssens28-Apr-04 2:20
Steven Wilssens28-Apr-04 2:20 
GeneralJust a note Pin
Lee Alexander16-Oct-03 6:24
Lee Alexander16-Oct-03 6:24 
GeneralRe: Just a note Pin
Heath Stewart20-Nov-03 2:18
protectorHeath Stewart20-Nov-03 2:18 
GeneralRe: Just a note Pin
Lee Alexander23-Nov-03 22:21
Lee Alexander23-Nov-03 22:21 
GeneralRe: Just a note Pin
Heath Stewart24-Nov-03 4:37
protectorHeath Stewart24-Nov-03 4:37 
GeneralRe: Just a note Pin
Lee Alexander28-Nov-03 0:12
Lee Alexander28-Nov-03 0:12 
GeneralRe: Just a note Pin
Santosh Rao17-Aug-05 20:18
Santosh Rao17-Aug-05 20:18 
GeneralRe: Just a note Pin
Lee Alexander17-Aug-05 21:37
Lee Alexander17-Aug-05 21:37 
GeneralRe: Just a note Pin
Santosh Rao17-Aug-05 22:32
Santosh Rao17-Aug-05 22:32 
GeneralRe: Just a note Pin
Santosh Rao17-Aug-05 22:29
Santosh Rao17-Aug-05 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.