如何使用 C# disable/override Windows 10 个热键
How to disable/override Windows 10 Hotkeys with C#
我正在尝试注册某些热键,但我不能,因为它们是 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.
如有必要,必须使用 c# 和 Win32 API 完成此操作。绝对不能用Autohotkey.
我找到的每一页都描述了如何使用 AutoHotKey 完成此操作。
我会 post 代码,但我真的不知道从哪里开始。我使用 Win32 来注册热键。我假设有一种方法可以覆盖它们,但我找不到任何信息。
有人知道吗?
可以使用键盘挂钩来完成此操作。可以在 CodeProject Article
上找到一个很好的钩子 class
使用下面的代码将阻止 WIN+LEFT 或 WIN+RIGHT 从发生。您可以使用它来覆盖您想要的任何键。
这甚至会覆盖您通过 RegisterHotKey
Win API 添加的热键。
在项目中拥有这些 class 后,您可以将处理程序添加到静态 HookManager
class,如下所示。
//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;
}
}
我正在尝试注册某些热键,但我不能,因为它们是 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.
如有必要,必须使用 c# 和 Win32 API 完成此操作。绝对不能用Autohotkey.
我找到的每一页都描述了如何使用 AutoHotKey 完成此操作。
我会 post 代码,但我真的不知道从哪里开始。我使用 Win32 来注册热键。我假设有一种方法可以覆盖它们,但我找不到任何信息。
有人知道吗?
可以使用键盘挂钩来完成此操作。可以在 CodeProject Article
上找到一个很好的钩子 class使用下面的代码将阻止 WIN+LEFT 或 WIN+RIGHT 从发生。您可以使用它来覆盖您想要的任何键。
这甚至会覆盖您通过 RegisterHotKey
Win API 添加的热键。
在项目中拥有这些 class 后,您可以将处理程序添加到静态 HookManager
class,如下所示。
//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;
}
}