Microsoft KB Archive/822492

= How to capture a pressed function key and then perform an action on a Windows application =

Article ID: 822492

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SUMMARY
This article describes how to capture a pressed function key and then perform an action on a Microsoft Windows application.

Function keys should be captured in the KeyDown event of the control. The event handler receives an argument of type KeyEventArgs. The KeyValue property of KeyEventArgs provides information about the key that you pressed. You can compare the KeyValue property with the Keys enumeration class constraints.

The Keys enumeration class specifies all the key codes and all the key modifiers. The Keys enumeration class contains constants that you can use to process the keyboard input. Keys are identified by the key values. A key value is made up of a key code and a set of modifiers that are combined in a single integer value. The four left digits of a key value contain the key code for the modifier keys (the SHIFT key, the CTRL key, and the ALT key), and the four right digits of a key value contain modifier bits for the modifier keys.

The modifier keys cannot be trapped in the KeyDown event of the control. Therefore, you should use the bitwise AND operator with the ModifierKeys property together with a value of the Keys enumeration class to determine what modifier key is in a pressed state. ModifierKeys is a shared member of the Control class. The Control.ModifierKeys property receives a value that indicates what modifier key is in a pressed state.



MORE INFORMATION
The following step-by-step example describes how to capture the key that is in a pressed state on Form1. This example also describes how to determine whether the key that is in a pressed state is a function key by comparing the key value with the value in the Keys enumeration class. When you press a function key, the specified action for the function key is performed in the FuncKeysModule procedure.  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application. Click OK.

By default, Form1 is created. Add a Button control and add a TextBox control to Form1.  Add the following code to the KeyDown event of TextBox1, to the KeyDown event of Form1, and to the KeyDown event of Button1: 'When the function keys are pressed, FuncKeysModule is called. If e.KeyValue = Keys.F1 Or Keys.F2 Or Keys.F3 Or Keys.F4 Or Keys.F5 Or Keys.F6 Or Keys.F7 Or Keys.F8 Or Keys.F9 Or Keys.F10 Or Keys.F11 Or Keys.F12 Then FuncKeysModule(e.KeyValue) e.Handled = True End If Note Write the KeyDown event handler for all the controls on the form to capture the function keys. Otherwise, the function keys are not captured when the focus is on the control. </li>  Add the following code after the Button1_KeyDown event handler: Public Sub FuncKeysModule(ByVal value As Keys) 'Check what function key is in a pressed state, and then perform the corresponding action. Select Case value Case Keys.F1               'Add the code for the function key F1 here. MessageBox.Show(&quot;F1 pressed&quot;) Case Keys.F2               'Add the code for the function key F2 here. MessageBox.Show(&quot;F2 pressed&quot;) Case Keys.F3               'Add the code for the function key F3 here. MessageBox.Show(&quot;F3 pressed&quot;) Case Keys.F4               'Add the code for the function key F4 here. MessageBox.Show(&quot;F4 pressed&quot;) Case Keys.F5               'Add the code for the function key F5 here. MessageBox.Show(&quot;F5 pressed&quot;) Case Keys.F6               'Add the code for the function key F6 here. MessageBox.Show(&quot;F6 pressed&quot;) Case Keys.F7               'Add the code for the function key F7 here. MessageBox.Show(&quot;F7 pressed&quot;) Case Keys.F8               'Add the code for the function key F8 here. MessageBox.Show(&quot;F8 pressed&quot;) Case Keys.F9               'Add the code for the function key F9 here. MessageBox.Show(&quot;F9 pressed&quot;) Case Keys.F10 'Add the code for the function key F10 here. MessageBox.Show(&quot;F10 pressed&quot;) Case Keys.F11 'Add the code for the function key F11 here. MessageBox.Show(&quot;F11 pressed&quot;) Case Keys.F12 'Add the code for the key F12 here MessageBox.Show(&quot;F12 pressed&quot;) End Select End Sub </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> Press any function key. Notice the output for the key.</li></ol>

<div class="references_section">