如何确定在KeyDown中按下了Shift + Tab [英] How can I determine in KeyDown that Shift + Tab was pressed

查看:366
本文介绍了如何确定在KeyDown中按下了Shift + Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在KeyDown中确定按下了 + Tab .

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
   }
}

不起作用,因为永远不会在同一秒内完全按下两个键.您总是首先要进行Shift,然后再进行另一个..

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.

您是正确的,您的代码无法正常工作,但是您的原因是错误的.问题在于 Tab 键具有特殊含义-它会导致焦点发生变化.您的事件处理程序未调用.

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.

如果您使用其他键代替 Tab ,则您的代码将正常工作.

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

如果您确实要更改某个控件的 Shift + Tab 的行为,则可以通过覆盖ProcessCmdKey但是请记住,许多用户使用 Tab 键在表单中导航,并且更改此键的行为可能会使这些用户烦恼.

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);
    }
}

这篇关于如何确定在KeyDown中按下了Shift + Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆