我可以用一个关键的热键制作WPF应用程序吗?到处都是使用修饰符制作热键的教程,例如:[Alt] + [X]。例如,我需要制作全局热键[F9]。我怎么能在C#.NET 4.5中做到这一点?
是的你可以。当您创建命令对象时,您可以传递的一个东西是InputGestureCollection
,其中包含从InputGesture
类下降的对象列表。其中一个类是KeyGesture
类,它定义了用于激活命令的特定键序列。
KeyGesture
类的一个属性是Key
property, of type Key
。这是关键值的枚举。你想要的是Key.F9
。
要创建Command,您可以执行以下操作:
InputGestureCollection myCommandGestures = new InputGesturecollection();
myCommandGestures.Add( new KeyGesture( Key.F9 );
RoutedCommand MyCommand = new RoutedCommand( "MyCommand", typeof( MyViewModelClass ), myCommandGestures );
编辑
以上内容仅适用于WPF。如果你想要一个全局热键,你将不得不调用Win32 RegisterHotKey
函数。 The answer to this question on the Visual Studio forum包含一个如何做到这一点的示例的链接。
我不确定您是否必须同时执行这两项操作或只是调用RegisterHotKey
函数来注册您的热键。