Export (0) Print
Expand All
3 out of 5 rated this helpful - Rate this topic

Control.PreviewKeyDown Event

Occurs before the KeyDown event when a key is pressed while focus is on this control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
'Declaration
Public Event PreviewKeyDown As PreviewKeyDownEventHandler

Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses. For example, by default, a Button control ignores the arrow keys. Pressing the arrow keys typically causes the focus to move to the previous or next control. The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.

For more information about handling events, see Consuming Events.

The following code example demonstrates a Button that includes a ContextMenuStrip. When the Button has the focus and you press the UP ARROW or DOWN ARROW keys, the ContextMenuStrip appears. The PreviewKeyDown event handler detects when the UP ARROW or DOWN ARROW keys are pressed and sets the IsInputKey property to true. This raises the KeyDown event so that you can display the ContextMenuStrip. You should not put any logic in the PreviewKeyDown event handler, other than to set the IsInputKey property. Instead, you should put your logic in the KeyDown event handler.

Public Class Form1

    Public Sub New()
        InitializeComponent()
        ' Form that has a button on it 
        AddHandler Button1.PreviewKeyDown, AddressOf Me.button1_PreviewKeyDown
        AddHandler Button1.KeyDown, AddressOf Me.button1_KeyDown
        Button1.ContextMenuStrip = New ContextMenuStrip
        ' Add items to ContextMenuStrip
        Button1.ContextMenuStrip.Items.Add("One")
        Button1.ContextMenuStrip.Items.Add("Two")
        Button1.ContextMenuStrip.Items.Add("Three")
    End Sub 

    ' By default, KeyDown does not fire for the ARROW keys 
    Private Sub button1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        Select Case (e.KeyCode)
            Case Keys.Down, Keys.Up
                If (Not (Button1.ContextMenuStrip) Is Nothing) Then
                    Button1.ContextMenuStrip.Show(Button1, _
                        New Point(0, Button1.Height), ToolStripDropDownDirection.BelowRight)
                End If 
        End Select 
    End Sub 

    ' PreviewKeyDown is where you preview the key. 
    ' Do not put any logic here, instead use the 
    ' KeyDown event after setting IsInputKey to true. 
    Private Sub button1_PreviewKeyDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs)
        Select Case (e.KeyCode)
            Case Keys.Down, Keys.Up
                e.IsInputKey = True 
        End Select 
    End Sub 

End Class

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.