This should be a lot simpler than it is having struggled with this for some time. I want to capture keystrokes on my Windows form. I have to be able to detect all of the ascii characters and whether the shift, alt or control key is pressed. I tried the usual keypress event, the keydown event but none of them get you all the characters. For example, the keydown event is great for getting the function keys, the shifted characters but it can't get characters like + or - Or can it? So I have tried the following:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.A))
{
MessageBox.Show("You pressed Ctrl+A!");
return true;
}
return false;
}
Great. This lets me get any key that is in the list of parameters that "Keys" provides. But that doesn't include characters like # + - and so on. With the debugger, most of the keys show up but not +,[, and so on, it shows multiple keys as being pressed and they make no sense. So whats the solution? Thanks
What about this? If you press two keys together your event KeyData should show you both keys that are pressed. Utilize the Properties Events in Windows Forms and select KeyDown.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Keys Pressed: " + e.KeyData.Tostring());
}