我如何注册一个全局热键说CTRL + SHIFT +(函)使用WPF和.NET 3.5? [英] How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?
问题描述
我使用WPF构建应用程序在C#。我怎么可以绑定到某些键?
另外,我怎么可以绑定到 Windows键?
我不知道你所说的全球性的意思是在这里,但这里有云(我假设你在应用层面的意思是一个命令,例如,保存所有的,可以从任何地方通过<大骨节病>控制骨节病> + <大骨节病>移骨节病> + <大骨节病>取值骨节病>)
您找到全局的UIElement
您的选择,例如,顶层窗口这是所有你需要这个绑定控件的父。由于冒泡的WPF事件,在子元素的事件将泡沫一路攀升到控制树的根。
现在,首先需要
- 要使用Key-组合绑定命令的
InputBinding
像这样 - 您就可以通过
的CommandBinding $ C联播的命令处理程序(如:code,它被称为
白水回收
) $ C>。
对于<大骨节病>窗口骨节病>键,你使用了正确的重点枚举成员, Key.LWin
或 Key.RWin
公共WindowMain()
{
的InitializeComponent();
//绑定密钥
InputBinding IB =新InputBinding(
MyAppCommands.SaveAll,
新KeyGesture(Key.S,ModifierKeys.Shift | ModifierKeys.Control));
this.InputBindings.Add(IB);
//绑定处理器
CB的CommandBinding =新的CommandBinding(MyAppCommands.SaveAll);
cb.Executed + =新ExecutedRoutedEventHandler(HandlerThatSavesEverthing);
this.CommandBindings.Add(CB);
}
私人无效HandlerThatSavesEverthing(对象obSender,ExecutedRoutedEventArgs E)
{
//这里做保存所有的事情。
}
I'm building an application in C# using WPF. How can I bind to some keys?
Also, how can I bind to the Windows key?
I'm not sure of what you mean by "global" here, but here it goes (I'm assuming you mean a command at the application level, for example, Save All that can be triggered from anywhere by Ctrl + Shift + S.)
You find the global UIElement
of your choice, for example, the top level window which is the parent of all the controls where you need this binding. Due to "bubbling" of WPF events, events at child elements will bubble all the way up to the root of the control tree.
Now, first you need
- to bind the Key-Combo with a Command using an
InputBinding
like this - you can then hookup the command to your handler (e.g. code that gets called by
SaveAll
) via aCommandBinding
.
For the Windows Key, you use the right Key enumerated member, Key.LWin
or Key.RWin
public WindowMain()
{
InitializeComponent();
// Bind Key
InputBinding ib = new InputBinding(
MyAppCommands.SaveAll,
new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control));
this.InputBindings.Add(ib);
// Bind handler
CommandBinding cb = new CommandBinding( MyAppCommands.SaveAll);
cb.Executed += new ExecutedRoutedEventHandler( HandlerThatSavesEverthing );
this.CommandBindings.Add (cb );
}
private void HandlerThatSavesEverthing (object obSender, ExecutedRoutedEventArgs e)
{
// Do the Save All thing here.
}
这篇关于我如何注册一个全局热键说CTRL + SHIFT +(函)使用WPF和.NET 3.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!