Detect the Tab Key Press in TextBox

34,253

Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses.

You can handle PreviewKeyDown event of your control to handle those key strokes and set them as input key.

private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if(e.KeyData == Keys.Tab)
    {
        MessageBox.Show("Tab");
        e.IsInputKey = true;
    }
    if (e.KeyData == (Keys.Tab | Keys.Shift))
    {
        MessageBox.Show("Shift + Tab");
        e.IsInputKey = true;
    }
}
Share:
34,253
user2102327
Author by

user2102327

Updated on August 02, 2022

Comments

  • user2102327
    user2102327 almost 2 years

    I am trying to detect the Tab key press in a TextBox. I know that the Tab key does not trigger the KeyDown, KeyUp or the KeyPress events. I found: Detecting the Tab Key in Windows Forms of BlackWasp in the internet. They suggest to override the ProcessCmdKey, which I did, but it does not get triggered either. Is there a reliable way to detect the Tab Key press?

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    
        bool baseResult = base.ProcessCmdKey(ref msg, keyData);
    
        if (keyData == Keys.Tab && textBox_AllUserInput.Focused)
        {
            MessageBox.Show("Tab key pressed.");
            return true;
        }
        if (keyData == (Keys.Tab | Keys.Shift) && textBox_AllUserInput.Focused)
        {
            MessageBox.Show("Shift-Tab key pressed.");
            return true;
        }
    
        return baseResult;
    }
    

    According to Cody Gray's suggestion, I changed the code as follows:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Tab && textBox_AllUserInput.Focused)
            {
                MessageBox.Show("Tab key pressed.");        }
            if (keyData == (Keys.Tab | Keys.Shift) && textBox_AllUserInput.Focused)
            {
                MessageBox.Show("Shift-Tab key pressed.");        }
    
            return base.ProcessCmdKey(ref msg, keyData);
        }
    

    The problem is that it does not capture the Tab key press.

  • Cody Gray
    Cody Gray about 8 years
    This is a hack. The actual place to handle those key strokes is in the control itself. Subclass TextBox and override IsInputKey.
  • Reza Aghaei
    Reza Aghaei about 8 years
    @CodyGray It's documented. Read the remarks section in documentation. When you can handle a Tab key this way why do you need to subclass.
  • Cody Gray
    Cody Gray about 8 years
    Sure, it is a documented hack. :-) Doesn't make it any less of a hack. There are lots of reasons to prefer subclassing, encapsulation, separation of concerns, and good design being chief among them. Reusability is another compelling concern. A subclass of TextBox would take maybe 3-4 more lines than this code, it's hardly a big effort.
  • Reza Aghaei
    Reza Aghaei about 8 years
    @CodyGray All that concepts are good things, separation of concerns, encapsulation, etc. But handling a Tab character is not a good reason to subclass a control specially in the case that the OP wants to handle that key for a certain TextBox control and run a logic after that. It can be handled simply using events :)
  • Cody Gray
    Cody Gray about 8 years
    Well, I disagree. The fact he is wanting to run specific logic afterwards is even more reason to subclass as far as I'm concerned. I'm not sure why you see subclassing as something undesirable to be avoided whenever possible. Anyway, you don't have to bend over backwards to please me, I didn't downvote this answer.
  • Reza Aghaei
    Reza Aghaei about 8 years
    @CodyGray The fact he is wanting to run specific logic afterwards is even more reason to subclass as far as I'm concerned. Suppose the OP wants to run a method ,F ,which is written in his form class, how should he run that method after he overrides ProcessCmdKey in his inherited TextBox? And of course, It's just a technical discussion and will be useful for the OP and future readers :)
  • user2102327
    user2102327 about 8 years
    Cody Gray, I am sorry but I am not that versed in C# yet. I do not know how to: Subclass TextBox and override IsInputKey.
  • Reza Aghaei
    Reza Aghaei over 4 years
    @user2102327 very old question, but I wonder if you could verify the solution?