开发者

Define application-wide hot key in mixed wpf/win32 app

开发者 https://www.devze.com 2023-01-28 20:18 出处:网络
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).

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 secon开发者_运维百科dary 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号