带有Ctrl键的KeyPressEventArgs.KeyChar的值 [英] Value of KeyPressEventArgs.KeyChar with ctrl key

查看:723
本文介绍了带有Ctrl键的KeyPressEventArgs.KeyChar的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KeyPressEvent上,我知道如何检测 CTRL 键何时按下,但是随后我想获取 CTRL + [什么?].

On a KeyPressEvent I know how to detect when the CTRL key is down, but then I want to get CTRL+[what?].

使用 CTRL + A 时,KeyChar = 1, CTRL + B 给出2,依此类推.检测 CTRL + a 输入的最佳方法是什么?

With CTRL+A, KeyChar = 1, CTRL+B gives 2, etc. What is the best way to detect CTRL+a input?

这是我的代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (ModifierKeys.HasFlag(Keys.Control))
    {
        Console.Write("(Ctrl) ");
    }
    Console.WriteLine(Convert.ToString(Convert.ToInt32(e.KeyChar)));
}

输入 a b CTRL + a CTRL + b 给出:

Entering a, b, CTRL+a, CTRL+b gives:

97
98
(Ctrl) 1
(Ctrl) 2

推荐答案

我的建议是使用

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

这篇关于带有Ctrl键的KeyPressEventArgs.KeyChar的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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