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# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
