Microsoft KB Archive/839201

= How to capture shortcut keys in Visual Studio .NET =

Article ID: 839201

Article Last Modified on 8/13/2007

-

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

-






 * Summary
 * Requirements
 * Microsoft Visual Basic .NET
 * Microsoft Visual C# .NET
 * Microsoft Visual C++ .NET
 * REFERENCES



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:  Open Visual Studio .NET. Create a new Visual Basic Windows Application project. Add a text box to the form.  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 = &quot;F&quot; 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(&quot;KeyPreview is True, and this is from the FORM.&quot;) 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 = &quot;F&quot; 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(&quot;KeyPreview is False, and this is from the CONTROL&quot;) 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 == &quot;F&quot;) {       // 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(&quot;KeyPreview is True, and this is from the FORM.&quot;); } } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.KeyCode.ToString == &quot;F&quot;) {       // 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(&quot;KeyPreview is False, and this is from the CONTROL.&quot;); } } 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:  Open Visual Studio .NET.</li> Create a new Visual C++ Windows Forms Application project.</li> Add two text boxes to the form.</li>  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,&quot;KeyPreview is True, and this came from the FORM&quot;); } } 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,&quot;KeyPreview is False, and this came from the CONTROL&quot;); } } </li></ol>

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:

https://solutionfinder.microsoft.com/

For additional information about the support options available from Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;[LN;CNTACTMS]

back to the top

<div class="references_section">