How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?(如何使用 WPF 和 .NET 3.5 注册全局热键以说出 CTRL+SHIFT+(LETTER)?)
问题描述
我正在使用 WPF 在 C# 中构建一个应用程序.如何绑定一些键?
I'm building an application in C# using WPF. How can I bind to some keys?
另外,如何绑定到 Windows 键?
推荐答案
我不确定你所说的全局"是什么意思.在这里,但在这里(我假设您的意思是应用程序级别的命令,例如 Save All 可以通过 Ctrl + Shift + S.)
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.)
您可以找到您选择的全局 UIElement,例如,顶层窗口是您需要此绑定的所有控件的父级窗口.由于冒泡"在 WPF 事件中,子元素上的事件将一直冒泡到控件树的根.
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.
现在,首先你需要
- 使用
InputBinding像这样将 Key-Combo 与命令绑定 - 然后您可以通过
CommandBinding将命令连接到您的处理程序(例如,由SaveAll调用的代码).
- to bind the Key-Combo with a Command using an
InputBindinglike this - you can then hookup the command to your handler (e.g. code that gets called by
SaveAll) via aCommandBinding.
对于 Windows 键,您使用正确的 Key 枚举成员,Key.LWin 或 Key.RWin
For the Windows Key, you use the right Key enumerated member, Key.LWin or Key.RWin
public WindowMain()
{
InitializeComponent();
// Bind Key
var ib = new InputBinding(
MyAppCommands.SaveAll,
new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control));
this.InputBindings.Add(ib);
// Bind handler
var 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.
}
这篇关于如何使用 WPF 和 .NET 3.5 注册全局热键以说出 CTRL+SHIFT+(LETTER)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 WPF 和 .NET 3.5 注册全局热键以说出 CTRL+SHIFT+(LETTER)?
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
