Control.IsInputKey(Keys) 方法

定義

判斷指定的按鍵是標準輸入按鍵或需要前置處理的特殊按鍵。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 值。One of the Keys values.

傳回

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

範例

下列程式碼範例將示範如何覆寫 TextBox 控制項的 IsInputKey 方法。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. 在此情況下,按鍵永遠不會到達 OnKeyDown 方法覆寫。In this case, the keypress never reaches the OnKeyDown method override. 為避免此預設行為,當使用者按下 TAB 鍵時,IsInputKey 方法覆寫會傳回 trueTo prevent this default behavior, the IsInputKey method override returns true when the user presses the TAB key. 對於所有其他 keypresses,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. 前置處理的按鍵包括 TAB、RETURN、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. 相關聯的列舉: UIPermissionWindowAllWindows 值。Associated enumeration: AllWindows value of UIPermissionWindow.

適用於

另請參閱