About override ProcessCMDKey in c# winform

前端 未结 1 1229
野的像风
野的像风 2020-12-22 08:37

I want my app to respond to left arrow and right arrow key. So I wrote

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

相关标签:
1条回答
  • 2020-12-22 08:52

    The debugger gets confused by the declaration of the Keys enum. Which looks like this:

    [Flags]
    public enum Keys {
        LButton = 1,
        ShiftKey = 0x10,
        ControlKey = 0x11,
        Control = 0x20000,
        // And lots more
    }
    

    With the [Flags] attribute turned on, the debugger visualizer tries to show the values of the individual bits in keyData. You pressed the Control key, Keys.ControlKey, whose value is 0x11. The Control flag is turned on because of that so keyData = 0x20011.

    So the debugger interprets 0x20011 as bits and makes it 0x20000 | 0x00010 | 0x00001. Which turns into "LButton | ShiftKey | Control". There isn't any good way to make it smarter, other than by using (int)keyData in the debugger expression. The fundamental issue is the [Flags] attribute on the enum, it is only somewhat appropriate but the vast majority of Keys enum values are not flag values.

    0 讨论(0)
提交回复
热议问题