在混合 wpf/win32 应用程序中定义应用程序范围的热键

发布于 2024-10-05 13:17:56 字数 477 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-10-12 13:17:56

我认为您可以尝试编写一个键盘钩子并附加此钩子来侦听来自应用程序线程的键盘消息。

这确实是一个很好的灵感示例: http://www.codeproject.com/KB/cs /globalhook.aspx

请注意,如果您使用 .Net 4.0,那么当您使用 SetWindowsHookEx 而不是用作

Assembly.GetExecutingAssembly().GetModules()[0]

hMod 参数时,请使用:

Process.GetCurrentProcess().MainModule.BaseAddress

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

Assembly.GetExecutingAssembly().GetModules()[0]

as a hMod param, use:

Process.GetCurrentProcess().MainModule.BaseAddress
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文