问题
On a KeyPressEvent
I know how to detect when the CTRL key is down, but then I want to get CTRL+[what?].
With CTRL+A, KeyChar
= 1, CTRL+B gives 2, etc. What is the best way to detect CTRL+a input?
Here is my code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (ModifierKeys.HasFlag(Keys.Control))
{
Console.Write("(Ctrl) ");
}
Console.WriteLine(Convert.ToString(Convert.ToInt32(e.KeyChar)));
}
Entering a, b, CTRL+a, CTRL+b gives:
97
98
(Ctrl) 1
(Ctrl) 2
回答1:
My suggestion is to use KeyDown event rather then KeyPress, because KeyPress works with processed input. KeyDown works with "raw" data (not quite, but enough for your purpose). KeyDown event handler has a parameter which holds data you need: KeyEventArgs
来源:https://stackoverflow.com/questions/20590746/value-of-keypresseventargs-keychar-with-ctrl-key