How to get characters read from the keyboard?
I am trying to get the characters read from the keyboard into some variable for further manipulation. I have the following list that I wish to recognize if entered by a user.
List of keyboard entries:
- letters
[
]
~
^
- numbers
Part of the code:
void HookManager_KeyUp(object sender, KeyEventArgs e)
{
string test = e.KeyCode.ToString();
Here numbers, letters and square brackets work but the ^
which require shift key is read incorrectly. For eg. it reads ^ which is on key 6 as a string value of 6 and not ^ as it should be. Here are the other readings
- [ = OemOpenBracket,
- ] = Oem6,
- ~ = OemTilde开发者_StackOverflow中文版,
- ^ = D6.
So D6 is not making senseAny help would be appreciated.
Thanks
AA
You need to check to see if e.Shift is true, in addition to just checking the KeyCode property.
It would be easier to do it this way:
private readonly string VALID_KEYS = "[]~^ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private void txtBox_KeyPress(object sender, KeyPressEventArgs e) { if (VALID_KEYS.IndexOf(char.ToUpper(e.KeyChar)) != -1 || e.KeyChar == (char)8) e.Handled = false; else e.Handled = true; }
This might be of some help...
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch (keyData)
{
case Keys.Shift | Keys.D6:
//Your code
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Thanks, Ram
精彩评论