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..
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);
}
}
If you are looking for a key press combination (Tab, then Shift) like Ctrl K + D you will have to use this modified example which was taken from MSDN social.
private StringBuilder _pressedKeys = new StringBuilder();
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
_pressedKeys.Append("Tab");
return;
}
if (e.Modifiers == Keys.Shift)
{
_pressedKeys.Append("Shift");
return;
}
if (_pressedKeys.ToString()."TabShift")
{
MessageBox.Show("It works!");
_pressedKeys.Clear();
}
else
{
_pressedKeys.Clear();
}
base.OnKeyDown(e);
}
First hook the Tab keypress event, then during the event, check the state of the Shift key. Keep in mind that there are two shift keys; make sure you check both of them.
This very related post shows how to check the state of modifier keys:
How to detect the currently pressed key?
Edit: an insight provided by another answerer who justly deserves an upvote is that the default behavior of the tab key (to change control focus) must be suppressed.
You can find your answer in
this post
It's Simple.
You can do that using KeyUp Event in the TextBox
private void txtBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
Or
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
It's Simple.
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}