Reputation: 31
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());
}
}
Upvotes: 2
Views: 3565
Reputation: 306
Not sure if you have had any luck.
But try this code:
switch (e.KeyData)
{
case Keys.Control:
{
if (e.KeyData == Keys.Subtract)
{ }
else if (e.KeyData == Keys.C)
{ }
break;
}
}
Upvotes: 0
Reputation: 4012
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.
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)
Upvotes: 6
Reputation: 62970
e.KeyCode
contains the key value + modifier info
e.KeyCode = e.KeyValue | e.Modifiers
Use e.KeyCode
Upvotes: 2