SoFunction
Updated on 2025-03-08

C# implements global shortcut key function

Using global shortcut keys in c#

Since .net does not provide a library for shortcut keys, this function must be implemented through the API.

In winapi, the global shortcut keys for registration and cancellation are respectively passedRegisterHotKeyandUnregisterHotKeyFunction implementation. It seems not concise to use this API directly in C#, here I provide a more friendly package.

The code is as follows:

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

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

        /// <summary> 
        /// Register shortcut key        /// </summary> 
        /// <param name="hWnd">Hold the handle of the shortcut key window</param>        /// <param name="fsModifiers">Key combination</param>        /// <param name="vk">Virtual key code for shortcut keys</param>        /// <param name="callBack">Callback function</param>        public static void Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack)
        {
            int id = keyid++;
            if (!RegisterHotKey(hWnd, id, fsModifiers, vk))
                throw new Exception("regist hotkey fail.");
            keymap[id] = callBack;
        }

        /// &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, );
            }
        }

        /// &lt;summary&gt; 
        /// Shortcut key message processing        /// &lt;/summary&gt; 
        public static void ProcessHotKey( m)
        {
            if ( == WM_HOTKEY)
            {
                int id = .ToInt32();
                HotKeyCallBackHanlder callback;
                if ((id, out callback))
                {
                    callback();
                }
            }
        }

        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
    }

Passed hereHotkey classImplementing the function packaging is very simple to use. The following is the reference test code.

        void Test()
        {
            ("Test");
        }

        protected override void WndProc(ref Message m)
        {
            (ref m);
            (m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (, Test);
        }

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