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. 在这种情况下,按键从不到达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. 对于所有其他 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

注解

调用方法以确定keyData参数指定的键是否是控件所需的输入键。 IsInputKeyCall 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. 关联的AllWindows UIPermissionWindow枚举: 值。Associated enumeration: AllWindows value of UIPermissionWindow.

适用于

另请参阅