我知道已经有很多关于如何使用 C#(使用 Ctrl、Alt 或其他任何东西)捕获全局热键的资源,但我还没有看到任何与 Windows 键一起使用的资源。
是否可以捕获程序并响应 Win+* 全局键盘快捷键?例如,如果用户按下Win+Z或类似的东西,则显示一个表单。
我知道已经有很多关于如何使用 C#(使用 Ctrl、Alt 或其他任何东西)捕获全局热键的资源,但我还没有看到任何与 Windows 键一起使用的资源。
是否可以捕获程序并响应 Win+* 全局键盘快捷键?例如,如果用户按下Win+Z或类似的东西,则显示一个表单。
Windows 键可以用作任何其他修饰符(const int MOD_WIN = 0x0008
根据MSDN)。我已经使用RegisterHotKey
基于 - 的代码对此进行了测试,并且工作正常。
更新
示例代码显示如何通过依赖RegisterHotKey
(LParam
手动收集的值)挂钩不同的键组合,包括 Windows 键:
[System.Runtime.InteropServices.DllImport("User32")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("User32")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int MOD_SHIFT = 0x4;
public const int MOD_CONTROL = 0x2;
public const int MOD_ALT = 0x1;
public const int WM_HOTKEY = 0x312;
public const int MOD_WIN = 0x0008;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY && m.WParam == (IntPtr)0)
{
IntPtr lParamWINZ = (IntPtr)5898248;
IntPtr lParamWINCTRLA = (IntPtr)4259850;
if (m.LParam == lParamWINZ)
{
MessageBox.Show("WIN+Z was pressed");
}
else if (m.LParam == lParamWINCTRLA)
{
MessageBox.Show("WIN+CTRL+A was pressed");
}
}
base.WndProc(ref m);
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
RegisterHotKey(this.Handle, 0, MOD_WIN, (int)Keys.Z);
RegisterHotKey(this.Handle, 0, MOD_WIN + MOD_CONTROL, (int)Keys.A);
}
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 0);
}
尽管上述解决方案似乎对某些人有用。它对我不起作用。使 WIN 键工作的唯一方法是在注册表中禁用它。
https://msdn.microsoft.com/en-us/library/bb521407(v=winembedded.51).aspx
缺点:所有 WIN 热键都被禁用。
在注册表中执行此操作有两种可能的方法:
(1)Systemwide:(这个我没测试过)HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\键盘布局,名称:“Scancode Map”,类型:REG_BINARY(二进制值),数值数据:“00 00 00 00 00 00 00 00 03 00 00 00 00 00 5B E0 00 00 5C E0 00 00 00 00"
(2)对于每个用户:(这是我使用的)HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer,名称:“NoWinKeys”,数据类型:REG_DWORD(DWORD值),值数据:0禁用限制, 或 1 启用限制