Introduction

Although the majority of developers nowadays develop a C# application on either ASP.NET or WPF or UWP, there are many times when developers either have to develop or maintain old-school WinForm (Windows Form) applications. This article walks you through effective ways you can capture your key events to program "shortcuts" in your Windows Form applications.

The Process of Programming Key Shortcuts

Enabling The KeyPreview Property

Make sure that the KeyPreview property of your WindowsForm is True such as:

Capture The KeyDown Event

Now, go to Events of your Form and program the KeyDown event. In the KeyDown event, you need to check what keys are being pressed and execute your desired code accordingly. You need to track the keys pressed so that you can program what code to execute when the correct combinations of keys (shortcut) are pressed by the user. 

For example, a popular shortcut which many people want in an application is the creation of a new window when the shortcut Ctrl + N is pressed. The following code provides you with the way you can configure that programmatically:

private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
 if(e.Modifiers == Keys.Control && e.KeyCode == Keys.N)
 {
 // Write your code for what you want for this shortcut (Ctrl+N) here
 }
}

Therefore, you can use KeyEventsArgs.Modifiers flag to track which combination of CTRL, SHIFT and ALT are being used along with other key being pressed. The same code can also be implemented in the following way:

private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
 if (e.Control && e.KeyCode == Keys.N)
 {
 // Your code to execute when shortcut Ctrl+N happens here
 }
}

By using KeyEventArgs.Control, you are directly checking whether Control (CTRL) key is being pressed or not, instead of checking which combination of CTRL, SHIFT and ALT has occurred, which was the case when we are using Modifiers flag. Therefore, you can directly check the keys CTRL/SHIFT/ALT are being used or not by checking the property KeyEventArgs.Control / KeyEventArgs.Shift / KeyEventArgs.Alt.

What if we need to use the shortcut of three keys being pressed such as CTRL+SHFT+O?

You can also track shortcuts when more than two keys being pressed, such as three keys, at the same time. The following code shows you how to program that in case you need it:

private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
 if (e.Control && e.Shift && e.KeyCode == Keys.O)
 {
 // Your code when shortcut Ctrl+Shft+O is pressed
 }
}

By using the preceding logic we can track three keys being pressed and execute our code.

Caution: Extra Hot, Take Note

Most developers face the same issue that even after applying the aforementioned logic their code is still not working (no action is occurring when shortcut is pressed) and if you find yourself in that hole then do not worry, the most probable reason is that you might have forgotten to set the KeyPreview property of the Form to True. So do not forget that step and you are good to go!

References