SoFunction
Updated on 2025-03-06

Implement global shortcut key function in WPF

Today I used the global shortcut keys in a small program and found the article I wrote before.Using global shortcut keys in c#After searching, I found that it is from WinForm version, and now I have mostly written WPF programs, so I translated it into WPF version.

    static class Hotkey
    {
        #region system api        [DllImport("")]
        [return: MarshalAs()]
        static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, uint vk);

        [DllImport("")]
        static extern bool UnregisterHotKey(IntPtr hWnd, int id);
        #endregion

        /// <summary>
        /// Register shortcut key        /// </summary>
        /// <param name="window">Hold shortcut key window</param>        /// <param name="fsModifiers">Key combination</param>        /// <param name="key">Shortcut keys</param>        /// <param name="callBack">Callback function</param>        public static void Regist(Window window, HotkeyModifiers fsModifiers, Key key, HotKeyCallBackHanlder callBack)
        {
            var hwnd = new WindowInteropHelper(window).Handle;
            var _hwndSource = (hwnd);
            _hwndSource.AddHook(WndProc);

            int id = keyid++;

            var vk = (key);
            if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk))
                throw new Exception("regist hotkey fail.");
            keymap[id] = callBack;
        }

        /// &lt;summary&gt;
        /// Shortcut key message processing        /// &lt;/summary&gt;
        static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WM_HOTKEY)
            {
                int id = wParam.ToInt32();
                if ((id, out var callback))
                {
                    callback();
                }
            }
            return ;
        }

        /// &lt;summary&gt;
        /// Log out shortcut key        /// &lt;/summary&gt;
        /// <param name="hWnd">Hold the handle of the shortcut key window</param>        /// <param name="callBack">Callback function</param>        public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
        {
            foreach (KeyValuePair&lt;int, HotKeyCallBackHanlder&gt; var in keymap)
            {
                if ( == callBack)
                    UnregisterHotKey(hWnd, );
            }
        }


        const int WM_HOTKEY = 0x312;
        static int keyid = 10;
        static Dictionary&lt;int, HotKeyCallBackHanlder&gt; keymap = new Dictionary&lt;int, HotKeyCallBackHanlder&gt;();

        public delegate void HotKeyCallBackHanlder();
    }

    enum HotkeyModifiers
    {
        MOD_ALT = 0x1,
        MOD_CONTROL = 0x2,
        MOD_SHIFT = 0x4,
        MOD_WIN = 0x8
    }

The code is still similar, and the way to use it is simpler:

protected override void OnSourceInitialized(EventArgs e)
{
    (this, HotkeyModifiers.MOD_ALT, , () =>
    {
        ("hello");
    });

    _context = new MainContext();
     = _context;
    _context.Process();
}

It should be noted that the callWhen functioning, the window needs to be assigned a handle, so it is recommended toOnLoadEvent orOnSourceInitializedPerformed in the function.

This is all about implementing global shortcut key functions in WPF. I hope it will be helpful to everyone's learning and I hope everyone will support me more.