Form.KeyPreview 속성

정의

포커스가 있는 컨트롤에 이벤트가 전달되기 전에 폼이 키 이벤트를 받을지 여부를 나타내는 값을 가져오거나 설정합니다.

public bool KeyPreview { get; set; }

속성 값

폼이 모든 키 이벤트를 받는 경우 true이고, 폼에서 현재 선택되어 있는 컨트롤이 키 이벤트를 받는 경우 false입니다. 기본값은 false입니다.

예제

다음 예제에서는 폼의 KeyPreview 속성을 true로 설정하고 양식 수준에서 키 이벤트를 처리하는 방법을 보여 줍니다. 예제를 실행하려면 다음 코드를 빈 양식에 붙여넣습니다.

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설정되면 양식은 모든 KeyPress, KeyDownKeyUp 이벤트를 받습니다. 폼의 이벤트 처리기가 키 입력 처리를 완료한 후 키 입력이 포커스를 사용하여 컨트롤에 할당됩니다. 예를 들어 속성이 로 설정 true 되고 현재 선택한 컨트롤이 TextBox인 경우 KeyPreview 키 입력이 폼 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

추가 정보