如何使用C#禁用/覆盖Windows 10热键

时间:2017-04-07 02:22:00

标签: c# windows-10 hotkeys

我试图注册某些热键,但我不能,因为它们是Windows的默认设置。

CTRL+WIN+1 minimizes the current window. I'd like it to do something else.
I'd like to completely disable WIN+LEFT/RIGHT.
I'm also trying to handle the CTRL+WIN+Arrow in my own virtual desktop manager.

zVirtualDesktop

如有必要,必须使用c#和Win32 API完成此操作。它绝对不能使用Autohotkey。

我发现的每个页面都说明了如何使用AutoHotKey完成此操作。

我发布了代码,但我真的不知道从哪里开始。我使用Win32注册热键。我假设有一种方法可以覆盖它们,但我无法找到任何信息。

有没有人有想法?

1 个答案:

答案 0 :(得分:4)

可以使用键盘钩子来完成此操作。可以在CodeProject Article

上找到一个好的钩子类

使用以下代码可防止发生 WIN + LEFT WIN + RIGHT 。您可以使用它来覆盖您想要的任何键。

这甚至会覆盖您通过HookManager Win API添加的热键。

在项目中拥有这些类之后,您可以将处理程序添加到静态//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; 类,如下所示。

KeyUp

您还可以添加鼠标事件,但为简单起见,我只是显示键盘钩子。

我还创建了一个通用列表,以便我知道哪些键当前已关闭,并从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; } } 事件列表中删除这些键。

commentjson