I have to use hotkeys which will be working from every window and pulpit. In winforms I used:
RegisterHotKey(this.Handle, 9000, 0x0002, (int)Keys.F10);
and
UnregisterHotKey(this.Handle, 9000);
and
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case 0x312:
switch (m.WParam.ToInt32())
{
case 9000:
//function to do
break;
}
break;
}
}
In my WPF aplication I tried do:
AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
and
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.F11 && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
//function to do
}
}
But it works only when my application is active and on the top, but it doesn't work when the application is minimized (for example). Is any method to do it?
You can use the same approach as in WinForms with some adaptation:
WindowInteropHelper
to getHWND
(instead ofHandle
property of a form)HwndSource
to handle window messages (instead of overridingWndProc
of a form)Key
enumeration fromWPF
- it's values are not the ones you wantComplete code: