开发者

Keys enumeration, not returning ASCII equivalent for all punctuation

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开发者_JAVA百科 getting my dllimport to work, just need to handle arrow keys and page up/down, perhaps I need both...


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜