How to capture shortcut keys in Visual Studio .NET (839201)



The information in this article applies to:

  • Microsoft Visual Studio, Enterprise Edition 6.0

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.

back to the top

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.

back to the top

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
    
back to the top

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.");
	}
}
back to the top

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 or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are 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 requirements. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, visit the following Microsoft Web site:For additional information about the support options available from Microsoft, visit the following Microsoft Web site: back to the top

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: back to the top

Modification Type:MajorLast Reviewed:12/15/2005
Keywords:kbHook kbEvent kbWindowsForms kbhowto KB839201 kbAudDeveloper