C#:充分利用KeyEventArgs“KEYDATA按下正确的键 [英] C#: Getting the correct keys pressed from KeyEventArgs' KeyData

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

问题描述

我诱捕的KeyDown 事件,我需要能够检查当前键是否按下如下:<大骨节病>控制 + <大骨节病>移 + <大骨节病> 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 ?


我知道我需要使用 e.KeyData KeyEventArgs 枚举和枚举标志和位的东西,但我不知道如何检查相结合。

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.

推荐答案

你需要使用的KeyEventArgs类的修饰符财产

喜欢的东西:

//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天全站免登陆