【问题标题】:C# and Arrow KeysC# 和箭头键
【发布时间】:2010-09-05 12:19:03
【问题描述】:

我是 C# 新手,正在现有应用程序中做一些工作。我有一个 DirectX 视口,其中包含我希望能够使用箭头键定位的组件。

目前我正在重写 ProcessCmdKey 并捕获箭头输入并发送 OnKeyPress 事件。这可行,但我希望能够使用修饰符(ALT+CTRL+SHIFT)。只要我持有一个修饰符并按下箭头,就不会触发我正在收听的任何事件。

有人对我应该去哪里有任何想法或建议吗?

【问题讨论】:

    标签: c# user-interface directx


    【解决方案1】:

    在您覆盖的 ProcessCmdKey 中,您如何确定按下了哪个键?

    keyData(第二个参数)的值将根据按下的键和任何修改键而改变,因此,例如,按下左箭头将返回代码 37,shift-left 将返回 65573,ctrl-left 将返回 131109 和alt-left 262181.

    您可以使用适当的枚举值提取修饰符和按下的键:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        bool shiftPressed = (keyData & Keys.Shift) != 0;
        Keys unmodifiedKey = (keyData & Keys.KeyCode);
    
        // rest of code goes here
    }
    

    【讨论】:

      【解决方案2】:

      我赞成Tokabi's answer,但对于比较密钥,*.com here 有一些额外的建议。以下是我用来帮助简化一切的一些函数。

         public Keys UnmodifiedKey(Keys key)
          {
              return key & Keys.KeyCode;
          }
      
          public bool KeyPressed(Keys key, Keys test)
          {
              return UnmodifiedKey(key) == test;
          }
      
          public bool ModifierKeyPressed(Keys key, Keys test)
          {
              return (key & test) == test;
          }
      
          public bool ControlPressed(Keys key)
          {
              return ModifierKeyPressed(key, Keys.Control);
          }
      
          public bool AltPressed(Keys key)
          {
              return ModifierKeyPressed(key, Keys.Alt);
          }
      
          public bool ShiftPressed(Keys key)
          {
              return ModifierKeyPressed(key, Keys.Shift);
          }
      
          protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
          {
              if (KeyPressed(keyData, Keys.Left) && AltPressed(keyData))
              {
                  int n = code.Text.IndexOfPrev('<', code.SelectionStart);
                  if (n < 0) return false;
                  if (ShiftPressed(keyData))
                  {
                      code.ExpandSelectionLeftTo(n);
                  }
                  else
                  {
                      code.SelectionStart = n;
                      code.SelectionLength = 0;
                  }
                  return true;
              }
              else if (KeyPressed(keyData, Keys.Right) && AltPressed(keyData))
              {
                  if (ShiftPressed(keyData))
                  {
                      int n = code.Text.IndexOf('>', code.SelectionEnd() + 1);
                      if (n < 0) return false;
                      code.ExpandSelectionRightTo(n + 1);
                  }
                  else
                  {
                      int n = code.Text.IndexOf('<', code.SelectionStart + 1);
                      if (n < 0) return false;
                      code.SelectionStart = n;
                      code.SelectionLength = 0;
                  }
                  return true;
              }
              return base.ProcessCmdKey(ref msg, keyData);
          }
      

      【讨论】: