英語で読む

次の方法で共有


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設定されている場合、フォームはすべての 、KeyDown、および イベントをKeyUp受け取りますKeyPress。 フォームのイベント ハンドラーがキーストロークの処理を完了すると、キーストロークがフォーカスを持つコントロールに割り当てられます。 たとえば、 プロパティが KeyPreviewtrue 設定され、現在選択されているコントロールが の場合、 TextBoxキーストロークがフォーム TextBox のイベント ハンドラーによって処理された後、コントロールは押されたキーを受け取ります。 フォーム レベルでのみキーボード イベントを処理し、コントロールがキーボード イベントを受信できないようにするには、フォームのKeyPressイベント ハンドラーの プロパティを にtrue設定KeyPressEventArgs.Handledします。

このプロパティを使用すると、アプリケーションのほとんどのキーストロークを処理し、キーストロークを処理するか、キーストロークを処理する適切なコントロールを呼び出すことができます。 たとえば、アプリケーションでファンクション キーを使用する場合、キーストローク イベントを受け取る可能性があるコントロールごとにコードを記述するのではなく、フォーム レベルでキーストロークを処理できます。

注意

フォームに表示コントロールまたは有効なコントロールがない場合は、すべてのキーボード イベントが自動的に受信されます。

注意

フォーム上のコントロールは、受け取ったキーストロークを取り消すためにプログラムできます。 コントロールはフォームにこれらのキーストロークを送信しないため、 の設定 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

こちらもご覧ください