How to capture shortcut keys in Visual Studio .NET

Article translations Article translations
Article ID: 839201 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

This article describes how to capture keyboard events in the Form object by using Microsoft Visual Studio .NET. The sample function demonstrates how to confirm that a user has entered a keyboard shortcut.


Requirements

This article assumes that you are familiar with the following topics:
  • Microsoft .NET events
  • Microsoft .NET forms
  • Microsoft .NET controls
The following examples show how to use Visual Studio .NET to confirm that the ALT key and the F key are pressed at the same time. This example also switches the KeyPreview property to show the object that is capturing the KeyDown event.

Note When the KeyPreview property is set to True, the form captures the KeyDown event before the control receives the KeyDown event.

Microsoft Visual Basic .NET

To verify that the ALT key and the F key are pressed at the same time, follow these steps:
  1. Open Visual Studio .NET.
  2. Create a new Visual Basic Windows Application project.
  3. Add a text box to the form.
  4. In the form, type the following code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' When the form loads, the KeyPreview property is set to True.
            ' This lets the form capture keyboard events before
            ' any other element in the form.
            Me.KeyPreview = True
        End Sub
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If e.Alt And e.KeyCode.ToString = "F" Then
                ' When the user presses both the 'ALT' key and 'F' key,
                ' KeyPreview is set to False, and a message appears.
                ' This message is only displayed when KeyPreview is set to True.
                Me.KeyPreview = False
                MsgBox("KeyPreview is True, and this is from the FORM.")
            End If
        End Sub
    
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If e.Alt And e.KeyCode.ToString = "F" Then
                ' When the user presses both the 'Alt key and the 'F' key,
                ' KeyPreview is set to True, and a message appears.
                ' This message is only displayed when KeyPreview is set to False.
                Me.KeyPreview = True
                MsgBox("KeyPreview is False, and this is from the CONTROL")
            End If
        End Sub
    

Microsoft Visual C# .NET

To verify that the ALT key and the F key are pressed at the same time, follow these steps:
  1. Open Visual Studio .NET.
  2. Create a new Visual C# Windows Application project.
  3. Add a text box to the form.
  4. In the form, type the following code:
private void Form1_Load(object sender, System.EventArgs e)
{
	// Set these when the form loads:
	// Have the form capture keyboard events first.
	this.KeyPreview = true;
	// Assign the event handler to the form.
	this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
	// Assign the event handler to the text box.
	this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
	if (e.Alt && e.KeyCode.ToString() == "F")
	{
		// When the user presses both the 'Alt' key and 'F' key,
		// KeyPreview is set to False, and a message appears.
		// This message is only displayed when KeyPreview is set to True.
		this.KeyPreview = false;
		MessageBox.Show("KeyPreview is True, and this is from the FORM.");
	} 
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
	if (e.Alt && e.KeyCode.ToString() == "F")
	{
		// When the user presses both the 'Alt' key and 'F' key,
		// KeyPreview is set to False, and a message appears.
		// This message is only displayed when KeyPreview is set to False.
		this.KeyPreview = true;
		MessageBox.Show("KeyPreview is False, and this is from the CONTROL.");
	}
}

Microsoft Visual C++ .NET

To verify that the ALT key and the F key are pressed at the same time, follow these steps:
  1. Open Visual Studio .NET.
  2. Create a new Visual C++ Windows Forms Application project.
  3. Add two text boxes to the form.
  4. In the form, type the following code:
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	// Set these when the form loads:
    	// Have the form capture keyboard events first.
    	this->KeyPreview = true;
    	// Assign the event handler to the form.
    	this->KeyDown += new System::Windows::Forms::KeyEventHandler(this, FormKeyDown);
    	// Assign the event handler to the text box.
    	this->textBox1->KeyDown += new System::Windows::Forms::KeyEventHandler(this, TextBoxKeyDown);
    }
    private: System::Void FormKeyDown(System::Object *  sender, System::Windows::Forms::KeyEventArgs *  e)
    {
    	if(e->Alt && e->KeyCode == System::Windows::Forms::Keys::F){
    		// When the user presses both the 'Alt' key and 'F' key,
    		// KeyPreview is set to False, and a message appears.
    		// This message is only displayed when KeyPreview is set to True.
    		this->KeyPreview = false;
    		MessageBox::Show(this,"KeyPreview is True, and this came from the FORM");
    	}
    }
    private: System::Void TextBoxKeyDown(System::Object *  sender, System::Windows::Forms::KeyEventArgs *  e)
    {
    	if(e->Alt && e->KeyCode == System::Windows::Forms::Keys::F){
    		// When the user presses both the 'Alt' key and 'F' key,
    		// KeyPreview is set to True, and a message appears.
    		// This message is only displayed when KeyPreview is set to False.
    		this->KeyPreview = true;
    		MessageBox::Show(this,"KeyPreview is False, and this came from the CONTROL");
    	}
    }
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

REFERENCES

For more information about the KeyPreview property, visit the following Microsoft Web site:
For more information about keyboard input terms, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/ms997478.aspx

Properties

Article ID: 839201 - Last Review: August 13, 2007 - Revision: 5.7
APPLIES TO
  • Microsoft Visual Studio 6.0 Enterprise Edition
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
Keywords: 
kbhook kbevent kbwindowsforms kbhowto KB839201

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com