Getting input from keyboard
Posted
by
SAMIR BHOGAYTA
on Samir ASP.NET with C# Technology
See other posts from Samir ASP.NET with C# Technology
or by SAMIR BHOGAYTA
Published on 2011-01-17T22:04:00.001-08:00
Indexed on
2011/01/28
23:35 UTC
Read the original article
Hit count: 473
Getting input from keyboard
The active application receives the input from the keyboard. This means the application has input focus.
There are two events for a key on a keyboard, when the key is pressed and when it is released. No it's not a single event as you might expect if you have no prior programming experience, in shooter games for example when you keep the forward key pressed (KeyDown) the player goes forward, and when it isn't pressed (KeyUp) the player stays put.
The event that occurs when the key is pressed is called KeyPress. It occurs between KeyDown and KeyUp, and therefore acts similar to KeyDown.
Similar to the way we handle OnPaint and other events we also handle the OnKeyDown event (because we want the event to occur when the key is pressed and not when it is released) by overriding it.
Try the code below and test it. You will understand the role of each property.
protected override void OnKeyDown(KeyEventArgs keyEvent)
{
// Gets the key code
lblKeyCode.Text = "KeyCode: " + keyEvent.KeyCode.ToString();
// Gets the key data; recognizes combination of keys
lblKeyData.Text = "KeyData: " + keyEvent.KeyData.ToString();
// Integer representation of KeyData
lblKeyValue.Text = "KeyValue: " + keyEvent.KeyValue.ToString();
// Returns true if Alt is pressed
lblAlt.Text = "Alt: " + keyEvent.Alt.ToString();
// Returns true if Ctrl is pressed
lblCtrl.Text = "Ctrl: " + keyEvent.Control.ToString();
// Returns true if Shift is pressed
lblShift.Text = "Shift: " + keyEvent.Shift.ToString();
}
How do I find out when the user presses a specific key?
As you probably imagine, this will be easily accomplished using 'if'.
if (keyEvent.KeyCode == Keys.A)
{
MessageBox.Show("'A' was pressed.");
}
Probably most beginners would be tempted to do this:
if (keyEvent.KeyCode == "A")
....
which is definitely incorrect because we can't compare System.Windows.Forms.Keys to a string.
Also note that in the example we are using 'keyEvent.KeyCode', that means that even if we have other shift keys pressed (Alt, Ctrl, Shift, Windows...) simultaneous with A, the if condition returns true because it doesn't recognize key combinations.
If we want to ignore key combinations (Alt+A, Ctrl+Shift+A), etc. we need to use 'keyEvent.KeyData' of course:
if (keyEvent.KeyData == Keys.A)
{
MessageBox.Show("'A', and only A, was pressed.");
}
When you right click on a file in Windows Explorer and you have the Shift key pressed you get the additional 'Open with...' item in the menu. This and many others are cases when you need to use the mouse button together with the keyboard.
The following code will change the background color of the form only if the form is clicked while the Ctrl key on the keyboard is pressed. If the Ctrl key is unpressed and the form is clicked nothing happens.
private void Form1_Click(object sender, System.EventArgs e)
{
Keys modKey = Control.ModifierKeys;
if(modKey == Keys.Control)
{
this.BackColor = Color.Yellow;
}
}
If you have further questions feel free to ask them and also check the following pages at MSDN:
KeyUp Event
KeyPress Event
KeyDown Event
© Samir ASP.NET with C# Technology or respective owner