基本上我有运行时加载的类,如下所示:
[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助手类?
或者我必须推出自己的热门课程吗?
这里的正确方法是什么?
答案 0 :(得分:2)
KeyEventArgs包含KeyData属性,其值为Keys,这是一个标志,意味着它可以是:
Keys keys = Keys.Control | Keys.F;
(你的热键)
如果以这种方式定义热键(属性)(而不是字符串),则可以比较键而不是字符串。
答案 1 :(得分:1)
System.Windows.Forms命名空间中有Keys枚举。然后对于热键Ctrl + A你可以这样写:
public class Action : Attribute {
public Keys HotKey {get;set;}
}
[Action(HotKey = (Keys.Control | Keys.A))]
public void MyMethod() {
...
}