ucef ucef - 1 year ago 76
C# Question

Catching MAJ + TAB click on textbox

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

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

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

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download