Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

c# - Global hotkeys in WPF working from every window

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the same approach as in WinForms with some adaptation:

  • use WindowInteropHelper to get HWND (instead of Handle property of a form)
  • use HwndSource to handle window messages (instead of overriding WndProc of a form)
  • don't use Key enumeration from WPF - it's values are not the ones you want

Complete code:

[DllImport("User32.dll")]
private static extern bool RegisterHotKey(
    [In] IntPtr hWnd,
    [In] int id,
    [In] uint fsModifiers,
    [In] uint vk);

[DllImport("User32.dll")]
private static extern bool UnregisterHotKey(
    [In] IntPtr hWnd,
    [In] int id);

private HwndSource _source;
private const int HOTKEY_ID = 9000;

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    var helper = new WindowInteropHelper(this);
    _source = HwndSource.FromHwnd(helper.Handle);
    _source.AddHook(HwndHook);
    RegisterHotKey();
}

protected override void OnClosed(EventArgs e)
{
    _source.RemoveHook(HwndHook);
    _source = null;
    UnregisterHotKey();
    base.OnClosed(e);
}

private void RegisterHotKey()
{
    var helper = new WindowInteropHelper(this);
    const uint VK_F10 = 0x79;
    const uint MOD_CTRL = 0x0002;
    if(!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL, VK_F10))
    {
        // handle error
    }
}

private void UnregisterHotKey()
{
    var helper = new WindowInteropHelper(this);
    UnregisterHotKey(helper.Handle, HOTKEY_ID);
}

private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    const int WM_HOTKEY = 0x0312;
    switch(msg)
    {
        case WM_HOTKEY:
            switch(wParam.ToInt32())
            {
                case HOTKEY_ID:
                    OnHotKeyPressed();
                    handled = true;
                    break;
            }
            break;
    }
    return IntPtr.Zero;
}

private void OnHotKeyPressed()
{
    // do stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...