【问题标题】:Is there a .NET class for hotkeys that can be matched against incoming KeyEventArgs?是否有可以与传入的 KeyEventArgs 匹配的热键的 .NET 类?
【发布时间】:2012-02-26 02:14:36
【问题描述】:

基本上我有运行时加载的类,如下所示:

[Plugin("Plugin name")]
class PluginActions
{
  [Action("Flip Normals")
  public void FlipNormals()
  {
    // code ....
  }

  [Action("Export .X object")
  public void ExportX()
  {
    // code ....
  }
}

这基本上是在设置了 onClick 事件处理程序的情况下向表单添加按钮。

现在我想使用属性以相同的样式指定 HOTKEY:

[Plugin("Plugin name")]
class PluginActions
{
  [Action("Flip Normals", Hotkey = "Ctrl+N")
  public void FlipNormals()
  {
    // code ....
  }

  [Action("Export .X object", Hotkey = "Ctrl+E")
  public void ExportX()
  {
    // code ....
  }
}

问题是,如何表示和捕获热键?作为字符串?可能有这样的课程吗?

void Form_KeyDown(object sender, KeyEventArgs e)
{
  // reflection magic ...

  foreach(var action in actionAttributes)
  {
    string action_hotkey = action.hotkey;

    /**** How to match KeyEventArgs against action_hotkey? ****/
  }
}

有处理热键的 .net 辅助类吗?

或者我是否必须推出自己的某种热键类?

这里的正确方法是什么?

【问题讨论】:

    标签: c# .net reflection plugins hotkeys


    【解决方案1】:

    KeyEventArgs 包含 KeyData 属性,它的值是 Keys,它是一个标志,意味着它可以是这样的:

    Keys keys = Keys.Control | Keys.F;(你的热键)

    如果您以这种方式定义热键(属性)(不使用字符串),则可以比较键而不是字符串。

    【讨论】:

      【解决方案2】:

      System.Windows.Forms 命名空间中有 Keys 枚举。然后对于热键 Ctrl+A 你可以这样写:

      public class Action : Attribute {
        public Keys HotKey {get;set;} 
      }
      
      
      [Action(HotKey = (Keys.Control | Keys.A))]
      public void MyMethod() {
        ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多