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…
If I remember correctly, the
KeyDownevent is used mostly for handling “special” keys, i.e. function keys, Home/End, etc.KeyCodeis 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
KeyPressevent instead ofKeyDown. However, if you also want to handle “special” keys, then you will need both.