如何确定是否邀请码是可打印字符 [英] How to determine if KeyCode is printable character
问题描述
我处理我的控制之一key down事件。如果按下的键是一个实际的输入(字母,数字或某种符号)我想将它添加到一个字符串我有。如果它是一个控制字符(回车,逃生等。)我不想做任何事情。
I am handling the key down event on one of my controls. If the key pressed is an actual input (alphabetic, numeric, or symbol of some kind) I want to append it to a string I have. If it is a control character (enter, escape, etc..) I don't want to do anything.
是否有一个快速简便的方法来确定关键的代码是一个可打印字符或控制字符?
Is there a quick and easy way to determine if the key code is a printable character or a control character?
目前我做的。
if (e.KeyCode == Keys.Enter)
{
e.Handled = false;
return;
}
不过,我知道有我不在乎大概几多个键该做点什么系统处理重要的,所以我不想来处理它们。
But I know there are probably a few more keys I don't care about that do something important in the system handler, so I don't want to handle them.
推荐答案
如果您只想打印字符,那么你可能不希望的onkeydown
。您应该使用的OnKeyPress
来代替,这就是所谓的只有的可打印字符(或多或少),以及组合键(Shift键适用的翻译,主要是)。
If you only want printable characters, then you probably don't want OnKeyDown
. You should be using OnKeyPress
instead, which is called only for printable characters (more or less), and applies translations for modifier keys (Shift, primarily).
我说:或多或少之上,因为你也将得到控制字符,这是不一定的打印的。这些可以很容易地 char.IsControl
,虽然过滤掉。
I said "more or less" above, because you will also get control characters, which aren't necessarily printable. Those can easily be filtered out with char.IsControl
, though.
protected override void OnKeyPress(KeyPressEventArgs e){
if(!char.IsControl(e.KeyChar)){
// do something with printable character
}
}
东西很容易在这样的设计忽略的是退格键。通常情况下,在收到退格字符的最好的行为('\b
或(char)的8
)是从您的缓冲区中删除最后一个字符。人们很容易忘记这样做。
Something that's easy to overlook in a design like this is the backspace key. Typically, the best behavior upon receiving a backspace character ('\b'
, or (char)8
) is to remove the last character from your buffer. It's easy to forget to do this.
这篇关于如何确定是否邀请码是可打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!