关于覆盖ProcessCMDKey在C#中的winform [英] About override ProcessCMDKey in c# winform

查看:1531
本文介绍了关于覆盖ProcessCMDKey在C#中的winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序左箭头,右箭头键响应。
所以我写了

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)

当我点击键盘左侧Ctrl键,
这个函数将被调用,并且KEYDATA的价值

When I click Ctrl on the left side of the keyboard, this function will be called, and the value of keyData is

keyData = LButton | ShiftKey | Control



这是为什么?

Why is that?

感谢你!

推荐答案

调试器获取的密钥枚举的声明相混淆。它看起来像这样:

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
}

随着[标记]属性开启时,调试器可视试图显示 KEYDATA 。您按下了Ctrl键,Keys.ControlKey,其值为0×11。该控制标记被打开,因为有这么KEYDATA = 0x20011

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.

因此​​,调试器解释0x20011称为比特,并使其地址0x20000 | 0x00010 | 0x00001。它变成LButton | Shift键|控制。还有就是不要让它比聪明,其他在调试器表达式中使用(INT)KEYDATA 什么好办法。根本问题是关于枚举的[标记]属性,它只是有点适当的,但绝大多数的密钥枚举值都没有标志值。

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.

这篇关于关于覆盖ProcessCMDKey在C#中的winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆