Getting input from keyboard

Getting the current pressed key on the keyboard, key combinations, key value and combining mouse clicks with key pressing.

When you type on the keyboard the keystrokes go to a particular application, the active application.
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

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top