Defining an application hotkey in a wpf / win32 mixed application.

I have a WPF application (prism) that uses many other WPF collections and even win32 dlls containing other windows (wpf and win32).

When the user presses a function key (F2-F12), I want to open the special functions of the program.

Something like that:

RoutedCommand commandF4 = new RoutedCommand(); commandF4.InputGestures.Add(new KeyGesture(Key.F4)); Application.Current.MainWindow.CommandBindings.Add(new CommandBinding(commandF4, CallCommandF4)); 

The problem is this: it only works if MainWindow has focus. If I open a secondary WPF window (or win32 window), the key binding is no longer applied.

Is there a way to add a global application hotkey for F4 (or some other key)?
Or at least the WPF hotkey?

+4
source share
1 answer

You can try, I think, write a hook on the keyboard and attach this hook to listen to keyboard messages from your application stream.

This is a really good example for inspiration: http://www.codeproject.com/KB/cs/globalhook.aspx .

Just note that if you are using .Net 4.0, then when will you use SetWindowsHookEx instead of using

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

as an hMod parameter, use:

 Process.GetCurrentProcess().MainModule.BaseAddress 
+3
source

All Articles