Tab Key Detection Click in the text box

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.

+11
source share
2 answers

Some keystrokes, such as the TAB , RETURN , ESC, and arrow keys, are usually ignored by some controls because they are not considered keystrokes.

You can handle the PreviewKeyDown event of your control to handle these keystrokes and set them as the enter 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; } } 
+14
source

you can use this code for tab ...

  private void input_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { //Check here tab press or not if (e.KeyCode == Keys.Tab) { //our code here } //Check for the Shift Key as well if (Control.ModifierKeys == Keys.Shift && e.KeyCode == Keys.Tab) { } } 
0
source

Source: https://habr.com/ru/post/1244811/


All Articles