C# 处理键盘事件时如何将 e.KeyChar 转换为实际字符值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15801358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:59:32  来源:igfitidea点击:

How do I convert e.KeyChar to an actual character value when handling keyboard events?

c#.netwinformskeypresskeyboard-events

提问by user1993843

All I'm wanting to do is get the actual character that is pressed whenever they key is entered for the event.

我想要做的就是获取在为事件输入键时按下的实际字符。

The only option I'm seeing is e.KeyCharwhich will give me the position rather than the character itself.

我看到的唯一选择是e.KeyChar它会给我位置而不是角色本身。

What I'm wanting to do is limit the values of the key press to digits between 0-9. The way I'm doing it right now is just if((e.KeyChar < 48 || (e.KeyChar > 57)))

我想要做的是将按键的值限制为 0-9 之间的数字。我现在的做法就是if((e.KeyChar < 48 || (e.KeyChar > 57)))

This seems a bit cheap to just put in the values for 0 and 9, and I'd love to know how to get the character value itself rather than its key.

仅仅输入 0 和 9 的值似乎有点便宜,我很想知道如何获取字符值本身而不是它的键。

EDIT: Thanks for the input; so in general is there no way to go from having the e.KeyCharvalue to the value of the input itself.

编辑:感谢您的输入;所以一般来说,没有办法从拥有e.KeyChar价值到输入本身的价值。

I'd really like to take my event, e, and access the character pressed directly as opposed to using e.KeyChar to get a numeric representation of that value.

我真的很想使用我的事件 e,并直接访问按下的字符,而不是使用 e.KeyChar 来获取该值的数字表示。

回答by Blorgbeard is out

KeyCharis a charvalue - it isthe character itself - 48 and 57 are the ascii values for 0and 9.

KeyChar是一个char值-它字符本身- 48和57都为ASCII值09

In C# you can refer to a character via its numeric ASCII code, or the actual character in single quotes. Therefore, your code can become:

在 C# 中,您可以通过数字 ASCII 代码或单引号中的实际字符来引用字符。因此,您的代码可以变成:

if((e.KeyChar < '0' || (e.KeyChar > '9')))

which is exactly the same thing.

这是完全一样的事情。

回答by e.beyer

Here is a simpler way of detecting if the key pressed is a digit or not:

这是检测按下的键是否为数字的更简单方法:

if (char.IsDigit(e.KeyChar)) { // doSomething(); }

回答by 2Toad

e.KeyCharis the charcorresponding to the key pressed, not the position. The .NET Framework uses charto represent a Unicode character. If by "actual character value", you mean the integral value of characters 0-9, and not their Unicode values (48-57) you can do this:

e.KeyCharchar对应于按下的键,而不是位置。.NET Framework 用于char表示 Unicode 字符。如果“实际字符值”是指字符 0-9 的整数值,而不是它们的 Unicode 值 (48-57),则可以执行以下操作:

var value = Char.GetNumericValue(e.KeyChar);

To limit the values of the key press to digits between 0-9:

要将按键值限制为 0-9 之间的数字:

if (!Char.IsDigit(e.KeyChar))
{
    e.Handled = true;
    return;
}

For a more complete solution, that allows corrections:

对于更完整的解决方案,允许更正:

const char Backspace = '\u0008';
const char Delete = '\u007f';

if (!Char.IsDigit(e.KeyChar)    // Allow 0-9
    && e.KeyChar != Backspace   // Allow backspace key
    && e.KeyChar != Delete)     // Allow delete key
{
    e.Handled = true;
    return;
}