共用方式為


Control.PreviewKeyDown 事件

定義

發生於焦點位於這個控制項上時並按下鍵盤按鍵的 KeyDown 事件之前。

public:
 event System::Windows::Forms::PreviewKeyDownEventHandler ^ PreviewKeyDown;
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 包含 的 ContextMenuStripButton當 有焦點,而且您按下向上鍵或向下鍵時,會出現 ContextMenuStripPreviewKeyDown事件處理常式會在按下向上鍵或向下鍵時偵測,並將 屬性設定 IsInputKeytrue 。 這會引發 KeyDown 事件,讓您可以顯示 ContextMenuStrip 。 您不應該將任何邏輯放在事件處理常式中 PreviewKeyDown ,而不是設定 IsInputKey 屬性。 相反地,您應該將邏輯放在事件處理常式中 KeyDown

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 鍵和方向鍵,因為它們不會被視為輸入按鍵按下。 例如,根據預設, Button 控制項會忽略方向鍵。 按下方向鍵通常會導致焦點移至上一個或下一個控制項。 方向鍵會被視為導覽鍵,而且按下這些按鍵通常不會引發 KeyDownButton 事件。 不過,按下 的 Button 方向鍵會引發 PreviewKeyDown 事件。 藉由處理 PreviewKeyDown 的 事件 Button ,並將 屬性設定 IsInputKeytrue ,您可以在按下方向鍵時引發 KeyDown 事件。 不過,如果您處理方向鍵,焦點將不再移至上一個或下一個控制項。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於