Control.PreviewKeyDown 事件

定义

在焦点位于此控件上的情况下,当有按键动作时发生(在 KeyDown 事件之前发生)。Occurs before the KeyDown event when a key is pressed while focus is on this control.

public:
 event System::Windows::Forms::PreviewKeyDownEventHandler ^ PreviewKeyDown;
public event System.Windows.Forms.PreviewKeyDownEventHandler PreviewKeyDown;
member this.PreviewKeyDown : System.Windows.Forms.PreviewKeyDownEventHandler 
Public Custom Event PreviewKeyDown As PreviewKeyDownEventHandler 

示例

下面的代码示例演示包含 ContextMenuStripButtonThe following code example demonstrates a Button that includes a ContextMenuStrip. Button 具有焦点并按向上键或向下箭头键时,将显示 ContextMenuStripWhen the Button has the focus and you press the UP ARROW or DOWN ARROW keys, the ContextMenuStrip appears. PreviewKeyDown 事件处理程序将在按向上键或向下箭头键时进行检测,并将 IsInputKey 属性设置为 trueThe PreviewKeyDown event handler detects when the UP ARROW or DOWN ARROW keys are pressed and sets the IsInputKey property to true. 这会引发 KeyDown 事件,以便显示 ContextMenuStripThis raises the KeyDown event so that you can display the ContextMenuStrip. 不应将任何逻辑放在 PreviewKeyDown 事件处理程序中,而不是设置 IsInputKey 属性。You should not put any logic in the PreviewKeyDown event handler, other than to set the IsInputKey property. 相反,应将逻辑放在 KeyDown 事件处理程序中。Instead, you should put your logic in the KeyDown event handler.

public Form1()
{
    InitializeComponent();

    // Form that has a button on it
    button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
    button1.KeyDown += new KeyEventHandler(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");
}

// By default, KeyDown does not fire for the ARROW keys
void button1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Down:
        case Keys.Up:
            if (button1.ContextMenuStrip != null)
            {
                button1.ContextMenuStrip.Show(button1,
                    new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight);
            }
            break;
    }
}

// PreviewKeyDown is where you preview the key.
// Do not put any logic here, instead use the
// KeyDown event after setting IsInputKey to true.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Down:
        case Keys.Up:
            e.IsInputKey = true;
            break;
    }
}
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

注解

某些按键(如选项卡、回车、ESC 和箭头键)通常会被某些控件忽略,因为它们不被视为输入键按下。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. 例如,默认情况下,Button 控件将忽略箭头键。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. 箭头键被视为导航键,按这些键通常不会引发 ButtonKeyDown 事件。The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. 不过,按下箭头键 Button 会引发 PreviewKeyDown 事件。However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. 通过处理 ButtonPreviewKeyDown 事件并将 IsInputKey 属性设置为 true,可以在按下箭头键时引发 KeyDown 事件。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 Handling and Raising Events.

适用于