Feng26

In my windows form app I added a MessageFilter and try to handle all the key strokes by capturing PreFilterMessage method's WindowsMessages.WM_KEYDOWN message. The strange thing is that it seems work fine for all the keys except for one, the F10 key. Bellow is the code for implementing the PreFilterMessage method:

...

public bool PreFilterMessage(ref Message objMessage)

{

if (objMessage.Msg == Convert.ToInt32(WindowsMessages.WM_KEYDOWN))

{

// My key handling logic

}

}

When the code gets executed, all other key strokes, except F10 key, get trapped and the control fall through the "My key handling logic". The F10 key, however, the key stroke seems never get noticed - actually, I checked it in debug mode and found that if statement never get hit when F10 is pressed.

What's going on here



Re: Windows Forms General PreFilterMessage of MessageFilter doesn't capture F10 key

Karthikeya Pavan Kumar .B

Check the Remarks section in the link WM_KEYDOWN Notification

Probable solution is Key events in datagrid control






Re: Windows Forms General PreFilterMessage of MessageFilter doesn't capture F10 key

nobugz

The F10 key is treated special by Windows, it operates like the Alt key. It generates the WM_SYSKEYDOWN. Fix your code like this:

public bool PreFilterMessage(ref Message m) {
if (m.Msg == 0x100 || m.Msg == 0x104) {
Console.WriteLine(m.ToString());
}
return false;
}

A better solution is to override the ProcessCmdKey() method so you don't have to bother with the actual Windows message:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData >= Keys.F1 && keyData <= Keys.F10)
Console.WriteLine("F{0}", (int)(keyData - Keys.F1) + 1);
return base.ProcessCmdKey(ref msg, keyData);
}





Re: Windows Forms General PreFilterMessage of MessageFilter doesn't capture F10 key

Pr.wa'el

you can use ProcessCmdKey Function in Windows Forms to handle any messages before goes to Form Controls as following : -

Code Snippet

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

'' Your Filter

Return MyBase.ProcessCmdKey(msg, keyData)

End Function

in this function you can set all keys filters you want , and they work will .I hope this will help you .




Re: Windows Forms General PreFilterMessage of MessageFilter doesn't capture F10 key

Feng26

Thanks nobugz! That WM_SYSKEYDOWN solved my problem.