Expand Minimize

Control.IsInputKey Method

Determines whether the specified key is a regular input key or a special key that requires preprocessing.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[UIPermissionAttribute(SecurityAction.InheritanceDemand, Window = UIPermissionWindow.AllWindows)]
protected virtual bool IsInputKey(
	Keys keyData
)

Parameters

keyData
Type: System.Windows.Forms.Keys

One of the Keys values.

Return Value

Type: System.Boolean
true if the specified key is a regular input key; otherwise, false.

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. If IsInputKey returns true, the specified key is sent directly to the control. If IsInputKey returns false, the specified key is preprocessed and only sent to the control if it is not consumed by the preprocessing phase. Keys that are preprocessed include the TAB, RETURN, ESC, and the UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys.

The following code example shows you how to override the IsInputKey method for a TextBox control. In this example, the TabTextBox class handles the TAB key. 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. By default, the TextBox control handles the TAB key by moving the input focus to the next control. In this case, the keypress never reaches the OnKeyDown method override. To prevent this default behavior, the IsInputKey method override returns true when the user presses the TAB key. 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);
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft