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

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

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.
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.

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

    By default, Form1 is created.
  6. Add a Button control and add a TextBox control to Form1.
  7. 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.
  8. 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
  9. On the Build menu, click Build Solution.
  10. On the Debug menu, click Start.
  11. Press any function key. Notice the output for the key.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keys(vs.71).aspx

Properties

Article ID: 822492 - Last Review: November 26, 2007 - Revision: 2.5
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005applies kbvs2005swept kbhowtomaster kbhowto kbwindowsforms kbforms KB822492

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