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.
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.
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 = TrueEnd 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.
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("F1 pressed") Case Keys.F2 'Add the code for the function key F2 here. MessageBox.Show("F2 pressed") Case Keys.F3 'Add the code for the function key F3 here. MessageBox.Show("F3 pressed") Case Keys.F4 'Add the code for the function key F4 here. MessageBox.Show("F4 pressed") Case Keys.F5 'Add the code for the function key F5 here. MessageBox.Show("F5 pressed") Case Keys.F6 'Add the code for the function key F6 here. MessageBox.Show("F6 pressed") Case Keys.F7 'Add the code for the function key F7 here. MessageBox.Show("F7 pressed") Case Keys.F8 'Add the code for the function key F8 here. MessageBox.Show("F8 pressed") Case Keys.F9 'Add the code for the function key F9 here. MessageBox.Show("F9 pressed") Case Keys.F10 'Add the code for the function key F10 here. MessageBox.Show("F10 pressed") Case Keys.F11 'Add the code for the function key F11 here. MessageBox.Show("F11 pressed") Case Keys.F12 'Add the code for the key F12 here MessageBox.Show("F12 pressed") End Select End Sub
On the Build menu, click Build Solution.
On the Debug menu, click Start.
Press any function key. Notice the output for the key.
For more information, visit the following Microsoft Developer Network (MSDN) Web site: