在混合 wpf/win32 应用程序中定义应用程序范围的热键
我有一个 WPF(棱镜)应用程序,它使用大量其他 WPF 程序集,甚至包含其他窗口(wpf 和 win32)的 win32 dll。
当用户按下功能键(F2-F12)时,我想打开特定的程序功能。
像这样的事情:
RoutedCommand commandF4 = new RoutedCommand();
commandF4.InputGestures.Add(new KeyGesture(Key.F4));
Application.Current.MainWindow.CommandBindings.Add(new CommandBinding(commandF4, CallCommandF4));
问题是:这仅在主窗口具有焦点时才有效。如果我打开辅助 WPF 窗口(或 win32 窗口),则键绑定不再适用。
有没有办法为 F4(或其他键)添加应用程序范围的全局热键?
或者至少是一个 WPF 范围的热键?
I've got a WPF (prism) app, which uses a whole lot of other WPF assemblies and even win32 dlls containing other windows (wpf and win32).
When the user presses a function Key (F2-F12) I want to open spezific program functions.
Something like this:
RoutedCommand commandF4 = new RoutedCommand();
commandF4.InputGestures.Add(new KeyGesture(Key.F4));
Application.Current.MainWindow.CommandBindings.Add(new CommandBinding(commandF4, CallCommandF4));
The problem is: this does only work while the MainWindow has the focus. If I open a secondary WPF window (or a win32 window) the keybinding does not apply anymore.
Is there any way to add an applicationwide global hotkey for F4 (or some other key)?
Or at least a WPF-wide hotkey?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以尝试编写一个键盘钩子并附加此钩子来侦听来自应用程序线程的键盘消息。
这确实是一个很好的灵感示例: http://www.codeproject.com/KB/cs /globalhook.aspx。
请注意,如果您使用 .Net 4.0,那么当您使用 SetWindowsHookEx 而不是用作
hMod 参数时,请使用:
You can try I think to write a hook to keyboard and attached this hook to listen for keyboard messages from your application's thread.
This is really good example for inspiration: http://www.codeproject.com/KB/cs/globalhook.aspx.
Just take a note that if you use .Net 4.0 then when you will use SetWindowsHookEx instead of using
as a hMod param, use: