C#:从KeyEventArgs'KeyData获取正确的按键 [英] C#: Getting the correct keys pressed from KeyEventArgs' KeyData

查看:983
本文介绍了C#:从KeyEventArgs'KeyData获取正确的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在捕获一个 KeyDown 事件,我需要能够检查当前按下的按键是: Ctrl + 转移 + M






我知道我需要使用 e.KeyData KeyEventArgs Keys 枚举,枚举标志和位,但我不确定如何检查组合。

解决方案

您需要使用修饰符属性。 p>

这样的东西:

  // asumming e是KeyEventArgs类型因为它是
//在KeyDown事件处理程序
// ..
bool ctrlShiftM; //如果Ctrl + Shit + M被按下将为true,否则为

ctrlShiftM =((e.KeyCode == Keys.M)&& //测试M按
((e.Modifiers& Keys.Shift)!= 0)&& //测试Shift修饰符
((e.Modifiers& Keys.Control)!= 0)); //测试Ctrl修饰符
如果(ctrlShiftM == true)
{
Console.WriteLine([Ctrl] + [Shift] + M被按下);
}


I am trapping a KeyDown event and I need to be able to check whether the current keys pressed down are : Ctrl + Shift + M ?


I know I need to use the e.KeyData from the KeyEventArgs, the Keys enum and something with Enum Flags and bits but I'm not sure on how to check for the combination.

解决方案

You need to use the Modifiers property of the KeyEventArgs class.

Something like:

//asumming e is of type KeyEventArgs (such as it is 
// on a KeyDown event handler
// ..
bool ctrlShiftM; //will be true if Ctrl + Shit + M is pressed, false otherwise

ctrlShiftM = ((e.KeyCode == Keys.M) &&               // test for M pressed
              ((e.Modifiers & Keys.Shift) != 0) &&   // test for Shift modifier
              ((e.Modifiers & Keys.Control) != 0));  // test for Ctrl modifier
if (ctrlShiftM == true)
{
    Console.WriteLine("[Ctrl] + [Shift] + M was pressed");
}

这篇关于C#:从KeyEventArgs'KeyData获取正确的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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