Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine in KeyDown that Shift + Tab was pressed

How can I determine in KeyDown that + Tab was pressed.

private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift)
   {
       //do stuff
   }
}

can't work, because never both keys are pressed exactly in the same second. You always to at first the Shift and then the other one..

like image 1000
hamze Avatar asked Oct 19 '25 20:10

hamze


1 Answers

It can't work, because never both keys are pressed exactly in the same second.

You're right that your code doesn't work, but your reason is wrong. The problem is that the Tab key has a special meaning - it causes the focus to change. Your event handler is not called.

If you use a different key instead of Tab, then your code will work fine.


If you really want to change the behaviour of Shift + Tab for one specific control, it can be done by overriding ProcessCmdKey but remember that many users use the Tab key to navigate around the form and changing the behaviour of this key may annoy those users.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (DateTimePicker_BirthDate.Focused && keyData == (Keys.Tab | Keys.Shift))
    {
        MessageBox.Show("shift + tab pressed");
        return true;
    }
    else
    {
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
like image 169
Mark Byers Avatar answered Oct 21 '25 10:10

Mark Byers