简体   繁体   English

C#KeyDown事件多个Keys加ControlKey

[英]C# KeyDown Event multiple Keys plus ControlKey

i want to recognize keystrokes to my Control. 我想识别我的Control的按键。 For this i use the KeyDown Event. 为此,我使用KeyDown事件。 The Kind of keystrokes I want to detect are something like CTRL + A or CTRL + C and so on. 我想要检测的击键类型是CTRL + A或CTRL + C等等。 (So combinations of multiple keys) (所以多个键的组合)

Now I have revied the KeyEventArgs and found the Keys enum. 现在我已经恢复了KeyEventArgs并找到了密钥枚举。 (Everything works perfect just use | and & to Combine and find the correct keys) An Example could be Shift + A then the Value of the KeyData Enum is: ShiftKey | (一切正常,只需使用|和&组合并找到正确的键)示例可以是Shift + A,然后KeyData枚举的值为:ShiftKey | Shift | 转移| A 一个

BUT

When i try it with the Control Key pressed (so Control + A) i got 131137 as Response? 当我按下控制键尝试它(所以控制+ A)我得到131137作为响应? And I do not know exactly why I do not get something like ControlKey | 而且我不知道为什么我没有得到类似ControlKey的东西 Control | 控制| A (or something like this) A(或类似的东西)

I have recogniced if I try it with A ist 131137 with B ist 131138 with C ist 131139 and so on ... So I think it is possible to calculate the key but I think there should be a better solution then just so something like this? 我已经认识到我是否尝试使用A ist 131137与B ist 131138与C ist 131139等等...所以我认为有可能计算密钥但我认为应该有一个更好的解决方案然后只是这样的事情?

131137 - 131072 = 65 (for A) 131137 - 131072 = 65(A)

Am I right, or is this the prevered solution, or do I misunderstand some Basic? 我是对的,还是这是预先解决的解决方案,还是我误解了一些基本的?

You can get Ctrl, Shift etc... using properties in KeyEventArgs object 您可以使用KeyEventArgs对象中的属性获取Ctrl,Shift等等

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs_properties(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs_properties(v=vs.90).aspx

void Control_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.F4)
    {
        // Be happy
    }
}

131072 == (int) Keys.Control 131072 ==(int)Keys.Control

so 所以

131137 (100000000001000001 binary) == (int) (Keys.Control | Keys.A) 131137(100000000001000001二进制)==(int)(Keys.Control | Keys.A)

and you can put something like that 你可以把这样的东西放进去

  private void myControl_KeyDown(object sender, KeyEventArgs e) {
    if (e.KeyData == (Keys.A | Keys.Control)) {
      ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM