检测C#中的多个同时按键 [英] Detecting multiple simultaneous keypresses in C#

查看:533
本文介绍了检测C#中的多个同时按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过检测某些无法键入的组合键(转义序列)(例如Ctrl + C,Ctrl + Z等)的按键来模拟C#中我的串行通信的超级终端功能.我理解这些键具有它们的ASCII等效项,并且可以照此发送.但是我在检测多个按键时面临问题.我的一些代码仅供参考:

I am looking to emulate hyperterminal functionality for my Serial Communication in C# by detecting the keypresses of certain key combinations (escape sequences) which cannot be typed out such as Ctrl+C, Ctrl+Z, etc. I understand that these keys have their ASCII equivalents and can be transmitted as such. But I am facing problems with the detection of multiple keypresses. Some of my code is provided as a reference :

private void Transmitted_KeyDown(object sender, KeyEventArgs e)
{


   if (e.Modifiers == Keys.Control || e.Modifiers== Keys.Shift || e.Modifiers==Keys.Alt)
   {
       var test = (char)e.KeyValue; // Only able to detect a single keypress!


       ComPort.Write(test.ToString());

   }
} 

推荐答案

如果您要查找常规键,则可以将其存储在列表中:在KeyDown上,将键添加到列表中.在向上键"上,将其从列表中删除.在KeyDown上,检查列表中的内容.

If you're looking for regular keys then you can store them in a list: On KeyDown, add the key to a list. On Key Up, remove it from the list. On KeyDown, check what's in the list.

但是,我不确定修饰键(例如ctrl,shift和alt)是否有keydown/keyup事件.对于那些人,您可以执行以下操作:

However, I'm not sure that there are keydown/keyup events for modifier keys like ctrl, shift, alt. For those you can do something like this:

bool CtrlDown = ((e.Modifiers & Keys.Control) > 0);
bool CtrlOnlyModifierDown = ((e.ModifierKeys & Keys.Control) == Keys.Control) 

这篇关于检测C#中的多个同时按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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