I am trying to detect a Tab key in a TextBox
. I know that the Tab key does not raise KeyDown
, KeyUp
or KeyPress
events. I found: Tab Key Detection in Windows Forms BlackWasp on the Internet. They suggest overriding the ProcessCmdKey, which I did, but it also does not start. Is there a reliable way to detect Tab key presses?
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; }
As suggested by Cody Gray, 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 Tab key presses.
source share