键枚举,不返回所有标点符号的 ASCII 等效值

发布于 2024-12-06 17:29:35 字数 803 浏览 1 评论 0原文

我有以下按键处理程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难如初 2024-12-13 17:29:35

如果我没记错的话, 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 of KeyDown. However, if you also want to handle "special" keys, then you will need both.

对你的占有欲 2024-12-13 17:29:35

键码不是 ASCII。

您可能想使用 KeyPress 事件而不是 KeyDownKeyPress 的事件参数包括 KeyChar 字段,该字段具有 ASCII 代码(或 Unicode,但对于 ASCII 字符,Unicode 值相同)。

Keycodes aren't ASCII.

You probably want to use the KeyPress event instead of KeyDown. The event args for KeyPress include the KeyChar field which has an ASCII code (or Unicode, but for ASCII characters the Unicode value is the same).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文