【问题标题】:How can I determine in KeyDown that Shift + Tab was pressed如何在 KeyDown 中确定按下了 Shift + Tab
【发布时间】:2012-06-24 10:35:51
【问题描述】:

如何在 KeyDown 中确定 + Tab 被按下。

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

不能工作,因为从来没有在同一秒内同时按下两个键。你总是先换档,然后换另一档..

【问题讨论】:

    标签: c# winforms keydown


    【解决方案1】:

    这是行不通的,因为永远不会同时按下两个键。

    您的代码不起作用是对的,但您的理由是错误的。问题在于 Tab 键具有特殊含义——它会导致焦点发生变化。未调用您的事件处理程序。

    如果您使用不同的键而不是 Tab,那么您的代码将正常工作。


    如果您真的想要更改 Shift + Tab 的行为以用于特定控件,可以通过覆盖 ProcessCmdKey 来完成但请记住,许多用户使用 Tab 键在表单中导航,更改此键的行为可能会惹恼这些用户。

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

    【讨论】:

      【解决方案2】:

      如果您正在寻找像 Ctrl K + D 这样的按键组合(Tab,然后是 Shift),您将不得不使用这个修改后的示例,该示例取自 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);
      }
      

      【讨论】:

      • _pressedKeys.Add 没有Add 我使用Append。在_pressedKeys.ToString()."TabShift"中有."TabShift"
      • 对不起,我用记事本写的,没有智能感知
      • 谢谢,还有一个问题:如果用户按住shift 键然后按Tab 怎么办?
      【解决方案3】:

      先钩住 Tab 按键事件,然后在事件期间,检查 Shift 键的状态。请记住,有两个 shift 键;确保同时检查它们。

      这篇非常相关的帖子展示了如何检查修饰键的状态:

      How to detect the currently pressed key?

      编辑:另一个值得一票的回答者提供的见解是必须禁止 Tab 键的默认行为(更改控制焦点)。

      【讨论】:

        【解决方案4】:

        您可以在 this post

        【讨论】:

          【解决方案5】:

          这很简单。

          您可以使用 TextBox 中的 KeyUp 事件来做到这一点

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

          【讨论】:

            【解决方案6】:

            这很简单。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-03-06
              • 1970-01-01
              • 2021-05-09
              • 1970-01-01
              相关资源
              最近更新 更多