Detecting multiple simultaneous key presses in C #
I want to mimic hyperterminal functionality for my serial communication in C # by detecting keystrokes of certain key combinations (escape sequences) that cannot be printed, such as Ctrl + C, Ctrl + Z, etc. I understand that these keys have their ASCII equivalents and can be passed as such. But I ran into problems with multiple keystrokes detection. Some of my codes are 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());
}
}
+2
source to share
3 answers
If you are looking for regular keys, you can store them in a list: In KeyDown, add the key to the list. In Key Up, remove it from the list. In KeyDown, check what's on the list.
However, I'm not sure if there are keydown / keyup keys for modifier keys like ctrl, shift, alt. For them, you can do something like this:
bool CtrlDown = ((e.Modifiers & Keys.Control) > 0);
bool CtrlOnlyModifierDown = ((e.ModifierKeys & Keys.Control) == Keys.Control)
+6
source to share