Control.IsInputKey(Keys) Control.IsInputKey(Keys) Control.IsInputKey(Keys) Control.IsInputKey(Keys) Method

定義

判斷指定的按鍵是標準輸入按鍵或需要前置處理的特殊按鍵。Determines whether the specified key is a regular input key or a special key that requires preprocessing.

protected:
 virtual bool IsInputKey(System::Windows::Forms::Keys keyData);
protected virtual bool IsInputKey (System.Windows.Forms.Keys keyData);
abstract member IsInputKey : System.Windows.Forms.Keys -> bool
override this.IsInputKey : System.Windows.Forms.Keys -> bool
Protected Overridable Function IsInputKey (keyData As Keys) As Boolean

參數

keyData
Keys Keys Keys Keys

其中一個 Keys 值。One of the Keys values.

傳回

如果指定的按鍵是標準輸入按鍵,則為 true,否則為 falsetrue if the specified key is a regular input key; otherwise, false.

範例

下列程式碼範例會示範如何覆寫IsInputKey方法TextBox控制項。The following code example shows you how to override the IsInputKey method for a TextBox control. 在此範例中,TabTextBox類別處理 TAB 鍵。In this example, the TabTextBox class handles the TAB key. TabTextBox有焦點,而且使用者按下 TAB 鍵在文字插入點,加入四個空格取代任何選取的文字。When the TabTextBox has the focus and the user presses the TAB key four spaces are added at the text insertion point, replacing any selected text. 根據預設,TextBox控制項處理 TAB 鍵,將輸入的焦點移至下一個控制項。By default, the TextBox control handles the TAB key by moving the input focus to the next control. 在此情況下,keypress 永遠不會到達OnKeyDown方法覆寫。In this case, the keypress never reaches the OnKeyDown method override. 若要防止這個預設行為,IsInputKey方法覆寫會傳回true當使用者按下 TAB 鍵。To prevent this default behavior, the IsInputKey method override returns true when the user presses the TAB key. 針對所有其他的按鍵IsInputKey方法覆寫會傳回呼叫方法的基底類別版本的結果。For all other keypresses, the IsInputKey method override returns the result of calling the base-class version of the method.

using System.Windows.Forms;

public class Form1 : Form
{
    public Form1()
    {
        FlowLayoutPanel panel = new FlowLayoutPanel();

        TabTextBox tabTextBox1 = new TabTextBox();
        tabTextBox1.Text = "TabTextBox";
        panel.Controls.Add(tabTextBox1);

        TextBox textBox1 = new TextBox();
        textBox1.Text = "Normal TextBox";
        panel.Controls.Add(textBox1);

        this.Controls.Add(panel);
    }
}

class TabTextBox : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        if (keyData == Keys.Tab)
        {
            return true;
        }
        else
        {
            return base.IsInputKey(keyData);
        }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyData == Keys.Tab)
        {
            this.SelectedText = "    ";                
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
}
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()

        Dim panel As New FlowLayoutPanel()

        Dim tabTextBox1 As New TabTextBox()
        tabTextBox1.Text = "TabTextBox"
        panel.Controls.Add(tabTextBox1)

        Dim textBox1 As New TextBox()
        textBox1.Text = "Normal TextBox"
        panel.Controls.Add(textBox1)

        Me.Controls.Add(panel)

    End Sub

End Class

Class TabTextBox
    Inherits TextBox

    Protected Overrides Function IsInputKey( _
        ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If keyData = Keys.Tab Then
            Return True
        Else
            Return MyBase.IsInputKey(keyData)
        End If

    End Function

    Protected Overrides Sub OnKeyDown( _
        ByVal e As System.Windows.Forms.KeyEventArgs)

        If e.KeyData = Keys.Tab Then
            Me.SelectedText = "    "
        Else
            MyBase.OnKeyDown(e)
        End If

    End Sub

End Class

備註

呼叫IsInputKey方法,以判斷是否由指定的索引鍵keyData參數是控制項要輸入索引鍵。Call the IsInputKey method to determine whether the key specified by the keyData parameter is an input key that the control wants. 在視窗訊息以判斷應該經過前置處理或直接傳送到控制項指定的輸入索引鍵前置處理期間呼叫這個方法。This method is called during window message preprocessing to determine whether the specified input key should be preprocessed or sent directly to the control. 如果IsInputKey傳回true,指定索引鍵會直接傳送至控制項。If IsInputKey returns true, the specified key is sent directly to the control. 如果IsInputKey傳回false,指定索引鍵是前置處理過的並只傳送至控制項,如果它不由前置處理階段。If IsInputKey returns false, the specified key is preprocessed and only sent to the control if it is not consumed by the preprocessing phase. 會前置處理過的索引鍵包括 [] 索引標籤、 傳回、 esc 鍵,以及向上、 向下鍵、 向左鍵和向右箭號的索引鍵。Keys that are preprocessed include the TAB, RETURN, ESC, and the UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys.

安全性

UIPermission
繼承類別呼叫這個方法的所有視窗。for all windows for inheriting classes to call this method. 相關聯的列舉型別:AllWindows的值UIPermissionWindowAssociated enumeration: AllWindows value of UIPermissionWindow.

適用於

另請參閱