Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.AddMessageFilter - how to read exactly what keys were pushed?

Tags:

c#

winforms

I'm experimenting with Application.AddMessageFilter, using some code written originally by Somebody Else, so I don't necessarily understand everything that's happening here.

Here's what the code looks like. In Main():

Application.AddMessageFilter(new KeyDownMessageFilter());

In KeyDownMessageFilter:

internal class KeyDownMessageFilter : IMessageFilter {
    private const int WM_KEYDOWN = 0x0100;

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_KEYDOWN)
        {
            var k = (Keys)m.WParam;
            var c = (char)k;
            // and some other stuff
        }
        return false;
    }
}

I can see that by casting m.WParam to a variable of type System.Windows.Forms.Keys and then casting that to a char, I can tell which key on the keyboard was pushed. So far, so good.

BUT - I cannot work out how to tell the difference between a Shifted key and a key push without a Shift - e.g. pushing % yields the character '5'. Even more weirdly, looking at the value of the k, it appears as "LButton | MButton | ShiftKey | Space" (no matter whether I push 5 or %).

The MSDN documentation on this subject is rather thin. Can anyone please explain how to tell exactly what character was pushed, and for bonus marks, explain what this Message object is supposed to be for, and why it uses such nondescript properties such as LParam and WParam to carry useful information?

like image 664
Shaul Behr Avatar asked Oct 17 '25 07:10

Shaul Behr


1 Answers

e.g. pushing "%" yields the character '5'

By design, the WM_KEYDOWN message passes a virtual key code in wparam. The virtual key code for the key that produces '%' and '5' is the same, it is the same key on your keyboard. It doesn't get turned into an actual typing key until Windows processes the WM_KEYDOWN message and turns it into a WM_CHAR message. Which will check the state of the Shift, Ctrl and Alt keys and change the generated character accordingly. The actual character that is produced depends on the active keyboard layout.

looking at the value of the k, it appears as "LButton | MButton | ShiftKey | Space"

That's a side-effect of the [Flags] attribute on the Keys type. The default Enum.ToString() method checks that attribute and will combine enum values if that attribute is present. The integer value of Keys.D5 is 0x35, a combination of 0x01 + 0x04 + 0x10 + 0x20. Respectively Keys.LButton, MButton, ShiftKey and Space. Clearly this is unhelpful in your case, cast to (int) in the watch expression.

Well, that explains what is going on. The one thing you really want to avoid is trying to translate the virtual key to a typing key yourself. Have a look at the ToUnicodeEx() Windows api function, the one you have to use to do it properly. You can use the Control.ModifierKeys property to detect the difference between a plain Keys.D5 and one that's pressed with the Shift key down.

Btw: you should also trap WM_SYSKEYDOWN, the message that's generated when the Alt key is down. Message 0x104.

like image 164
Hans Passant Avatar answered Oct 19 '25 22:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!