How to disable/override Windows 10 Hotkeys with C#

杀马特。学长 韩版系。学妹 提交于 2019-11-28 14:31:06

It is possible to do this using a keyboard hook. A good hook class for this can be found on this CodeProject Article

Using the below code will prevent the WIN+LEFT or WIN+RIGHT from occurring. You can use this to override whichever keys you'd like.

This will even override hotkeys which you added via RegisterHotKey Win API.

Once you have those classes in your project you can add handlers to the static HookManager class like below.

//It's worth noting here that if you subscribe to the Key_Press event then it will break the international accent keys.
HookManager.KeyPress += HookManager_KeyPress;
HookManager.KeyDown += HookManager_KeyDown;
HookManager.KeyUp += HookManager_KeyUp;

You can also add mouse events, but for simplicity I'm just showing the keyboard hook.

I've also created a generic list so that I know which keys are currently down and I remove those keys from the list on the KeyUp event.

public static List<Keys> keysDown = new List<Keys>();
private static void HookManager_KeyDown(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            if(keysDown.Contains(e.KeyCode) == false)
            {
                keysDown.Add(e.KeyCode);
            }

            if (e.KeyCode == Keys.Right && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }
            else if (e.KeyCode == Keys.Left && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }

        }

        private static void HookManager_KeyUp(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            while(keysDown.Contains(e.KeyCode))
            {
                keysDown.Remove(e.KeyCode);
            }
        }

        private static void HookManager_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Used for overriding the Windows default hotkeys

        }

        public static bool CTRL()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LControlKey) || 
                keysDown.Contains(Keys.RControlKey) || 
                keysDown.Contains(Keys.Control) || 
                keysDown.Contains(Keys.ControlKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool SHIFT()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LShiftKey) || 
                keysDown.Contains(Keys.RShiftKey) ||
                keysDown.Contains(Keys.Shift) ||
                keysDown.Contains(Keys.ShiftKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool WIN()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LWin) || 
                keysDown.Contains(Keys.RWin))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ALT()
    {
        //return keysDown.Contains(Keys.LShiftKey)
        if (keysDown.Contains(Keys.Alt) ||
            keysDown.Contains(Keys.LMenu) ||
            keysDown.Contains(Keys.RMenu))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!