Value of KeyPressEventArgs.KeyChar with ctrl key

為{幸葍}努か 提交于 2019-12-24 01:27:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!