键枚举,不返回所有标点符号的 ASCII 等效值
我有以下按键处理程序:
void Form1::texBox_KeyDown(System::Object^ sender,
System::Windows::Forms::KeyEventArgs^ e) {
//New lines in response to suggestion of using keypress
if (Control::ModifierKeys == Keys::Alt) return;
e->SuppressKeyPress=true;
unsigned char chr = (unsigned char)e->KeyCode;
//char chr = (char)e->KeyCode; //Gives negative 'values'
if (chr < ' ') return;
//else do stuff
}
它可以适当地处理数字和字母,但是当我按下任何标点符号时,按键代码会完全进入思维状态。使用签名 char
我得到 -66 的 '.'和 190 与 unsigned char
。
我认为这一定是由于我在 Windows 上搞乱了一些东西,请问有人可以提供更好的方法来处理表单标准文档容器之外的文本键盘吗?
按键听起来不错,但是它可以抑制输出吗?也许甚至是“Alt”检测(只是为了真正路由方便的 alt-F4 组合)?请参阅我在方法入口点添加的两行。 KeyPress 比让我的 dllimport 工作更容易,只需要处理箭头键和向上/向下翻页,也许我两者都需要......
I have the following key handler:
void Form1::texBox_KeyDown(System::Object^ sender,
System::Windows::Forms::KeyEventArgs^ e) {
//New lines in response to suggestion of using keypress
if (Control::ModifierKeys == Keys::Alt) return;
e->SuppressKeyPress=true;
unsigned char chr = (unsigned char)e->KeyCode;
//char chr = (char)e->KeyCode; //Gives negative 'values'
if (chr < ' ') return;
//else do stuff
}
This handles numbers and letters appropriately, but when I press any punctuation the KeyCodes go completely mental. Using signed char
I got -66 for '.' and 190 with unsigned char
.
I assume this must be due to something I messed with with Windows, please would someone offer a better way to handle textual keyboard outside of a Forms' standard document containers?
Keypress sounds good, will it work to supress output though? Maybe even 'Alt' detection (just to route the handy alt-F4 combo really)? Please see the two lines I added at method's entry point. KeyPress is easier than getting my dllimport to work, just need to handle arrow keys and page up/down, perhaps I need both...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话,
KeyDown
事件主要用于处理“特殊”键,即功能键、Home/End 等。KeyCode
是实际的键盘(硬件) “扫描码”,不保证与Unicode字符值相同。如果您需要字符值,您可能需要
KeyPress
事件而不是KeyDown
。但是,如果您还想处理“特殊”键,那么您将需要两者。If I remember correctly, the
KeyDown
event is used mostly for handling "special" keys, i.e. function keys, Home/End, etc.KeyCode
is the actual keyboard (hardware) "scan code", which is not guaranteed to be the same as the Unicode character value.If you want the character values, you probably want the
KeyPress
event instead ofKeyDown
. However, if you also want to handle "special" keys, then you will need both.键码不是 ASCII。
您可能想使用
KeyPress
事件而不是KeyDown
。KeyPress
的事件参数包括KeyChar
字段,该字段具有 ASCII 代码(或 Unicode,但对于 ASCII 字符,Unicode 值相同)。Keycodes aren't ASCII.
You probably want to use the
KeyPress
event instead ofKeyDown
. The event args forKeyPress
include theKeyChar
field which has an ASCII code (or Unicode, but for ASCII characters the Unicode value is the same).