Form.KeyPreview 属性

定义

获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。

C#
public bool KeyPreview { get; set; }

属性值

如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。 默认值为 false

示例

以下示例演示如何将窗体的 KeyPreview 属性设置为 true 并在窗体级别处理关键事件。 若要运行该示例,请将以下代码粘贴到空白窗体中。

C#
using System.Windows.Forms;

public class Form1 :
    System.Windows.Forms.Form

// Declare the controls contained on the form.
{
    private MyMnemonicButton button1;
    internal System.Windows.Forms.ListBox ListBox1;

    public Form1() : base()
    {
        // Set KeyPreview object to true to allow the form to process 
        // the key before the control with focus processes it.
        this.KeyPreview = true;

        // Add a MyMnemonicButton.  
        button1 = new MyMnemonicButton();
        button1.Text = "&Click";
        button1.Location = new System.Drawing.Point(100, 120);
        this.Controls.Add(button1);

        // Initialize a ListBox control and the form itself.
        this.ListBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        this.ListBox1.Location = new System.Drawing.Point(8, 8);
        this.ListBox1.Name = "ListBox1";
        this.ListBox1.Size = new System.Drawing.Size(120, 95);
        this.ListBox1.TabIndex = 0;
        this.ListBox1.Text = "Press a key";
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.ListBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

        // Associate the event-handling method with the
        // KeyDown event.
        this.KeyDown += new KeyEventHandler(Form1_KeyDown);
    }

    // The form will handle all key events before the control with  
    // focus handles them.  Show the keys pressed by adding the
    // KeyCode object to ListBox1. Ensure the processing is passed
    // to the control with focus by setting the KeyEventArg.Handled
    // property to false.
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        ListBox1.Items.Add(e.KeyCode);
        e.Handled = false;
    }

    [System.STAThreadAttribute]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}

// This button is a simple extension of the button class that overrides
// the ProcessMnemonic method.  If the mnemonic is correctly entered,  
// the message box will appear and the click event will be raised.  
public class MyMnemonicButton : Button
{
    // This method makes sure the control is selectable and the 
    // mneumonic is correct before displaying the message box
    // and triggering the click event.
    protected override bool ProcessMnemonic(char inputChar)
    {
        if (CanSelect && IsMnemonic(inputChar, this.Text))
        {
            MessageBox.Show("You've raised the click event " +
                "using the mnemonic.");
            this.PerformClick();
            return true;
        }
        return false;
    }
}

注解

当此属性设置为 true时,窗体将接收所有 KeyPressKeyDownKeyUp 事件。 表单的事件处理程序完成键击处理后,键击将分配给具有焦点的控件。 例如,如果 KeyPreview 属性设置为 true ,并且当前所选控件是 , TextBox在窗体的事件处理程序处理击键后, TextBox 控件将收到按下的键。 若要仅在窗体级别处理键盘事件,而不允许控件接收键盘事件,请将窗体事件处理程序KeyPress中的 属性设置为 。KeyPressEventArgs.Handledtrue

可以使用此属性来处理应用程序中的大多数击键,并处理击键或调用相应的控件来处理击键。 例如,当应用程序使用函数键时,你可能希望在窗体级别处理击键,而不是为每个可能接收击键事件的控件编写代码。

备注

如果窗体没有可见或已启用的控件,它会自动接收所有键盘事件。

备注

窗体上的控件可以编程以取消它收到的任何击键。 由于控件从不向窗体发送这些击键,因此无论 的设置 KeyPreview如何,窗体都永远不会看到它们。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅