Control.PreviewKeyDown Control.PreviewKeyDown Control.PreviewKeyDown Control.PreviewKeyDown Event

定義

發生於焦點位於這個控制項上時並按下鍵盤按鍵的 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 

範例

下列Button程式碼範例將示範ContextMenuStrip包含的。The following code example demonstrates a Button that includes a ContextMenuStrip. 當具有焦點,而且您按向上鍵或向下鍵時ContextMenuStrip ,就會出現。 ButtonWhen the Button has the focus and you press the UP ARROW or DOWN ARROW keys, the ContextMenuStrip appears. 事件處理常式會偵測何時按下向上箭號或向下鍵,並IsInputKey將屬性設定為truePreviewKeyDownThe PreviewKeyDown event handler detects when the UP ARROW or DOWN ARROW keys are pressed and sets the IsInputKey property to true. 這會引發KeyDown事件,讓您可以ContextMenuStrip顯示。This 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

備註

某些控制項通常會忽略一些按鍵,例如 TAB、RETURN、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. 方向鍵會視為導覽鍵,而按下這些按鍵通常不會引發KeyDownButton事件。The arrow keys are considered navigation keys and pressing these keys typically do not raise the KeyDown event for a Button. 不過,按的方向鍵ButtonPreviewKeyDown引發事件。However, pressing the arrow keys for a Button does raise the PreviewKeyDown event. 藉由處理PreviewKeyDown的事件Button並將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.

適用於