ucef ucef - 4 days ago 5
C# Question

Catching MAJ + TAB click on textbox

I have to catch the event when I pressed on Shift Tab in

TextBox
to write some code. It is possible to do that? I tried with that test on
KeyUp
event :

private void txtJustifTampon_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && Control.ModifierKeys == Keys.ShiftKey)
{
//do stuff
}
}

Answer

One of the possible ways out is to use PreviewKeyDown instead of KeyUp since

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

private void txtJustifTampon_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
  // If Shift + Tab pressed (i.e. Tab with Shift modifier) 
  if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift) {
    //TODO: put relevant code here (do stuff)
  }
}

Please, notice that we should use Keys.Shift (not Keys.ShiftKey) as the modifier and we should apply modifier to the event argument (e.Modifiers)

Comments