使用LowLevelKeyboardHook使用Alt Tab或者使用RegisterHotKey来Win Some Ubounded Key来使用Alt Tab键入快捷方式.只有使用LowLevelKeyboardHook的Win键才会出现问题.
在下面的示例中,当检测到Win Tab组合时,我接管了Win up事件.这样就可以使得以后的按键行为就像Win键仍然处于关闭状态.
private static IntPtr HookCallback(int nCode,IntPtr wParam,IntPtr lParam) { if (nCode != HC_ACTION) return CallNextHookEx(_hookID,nCode,wParam,lParam); var keyInfo = (Kbdllhookstruct)Marshal.PtrToStructure(lParam,typeof(Kbdllhookstruct)); if (keyInfo.VkCode == VK_LWIN) { if (wParam == (IntPtr)WM_KEYDOWN) { _isWinDown = true; } else { _isWinDown = false; if (_isWinTabDetected) { _isWinTabDetected = false; return (IntPtr)1; } } } else if (keyInfo.VkCode == VK_TAB && _isWinDown) { _isWinTabDetected = true; if (wParam == (IntPtr)WM_KEYDOWN) { return (IntPtr)1; } else { _isWinTabDetected = true; Console.WriteLine("WIN + TAB Pressed"); return (IntPtr)1; } } return CallNextHookEx(_hookID,lParam); } } }
您可以在这里找到完整的代码(请注意,它应该在一个空的WinForms项目中替换您的Program.cs以运行):https://gist.github.com/christianrondeau/bdd03a3dc32a7a718d62 – 按Win Tab,每次按下快捷方式时,窗体标题应该更新.
Note that the intent of hooking into this specific combination is to provide an Alt + Tab alternative without replacing Alt + Tab itself. An answer providing the ability to launching custom code using Win + Tab will also be accepted.
这是我的想法,我找不到文档.所有这些都可能成功地回答我的问题.
>告诉Windows“取消”Win而不实际触发它
>防止Windows启动“开始”菜单一次
>直接钩在Windows的Win事件中,而不是手动钩入击键(这将是我的第一选择,如果存在)
解决方法
当您的应用失去焦点时,请慎重并注销此KB钩子!
[DllImport("user32.dll")] static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); private static IntPtr HookCallback(int nCode,IntPtr lParam) { if (nCode == HC_ACTION) { var keyInfo = (Kbdllhookstruct) Marshal.PtrToStructure(lParam,typeof (Kbdllhookstruct)); if ((int) wParam == WM_KEYDOWN && keyInfo.VkCode == VK_TAB && (GetAsyncKeyState(Keys.LWin) < 0 || GetAsyncKeyState(Keys.RWin) < 0)) { _mainForm.Text = "Win + Tab was pressed " + (++_winTabPressCounter) + " times"; return (IntPtr) 1; } } return CallNextHookEx(_hookID,lParam); }
在发现这种技术之前,我尝试过几件事情.
这篇文章是最有帮助的https://stackoverflow.com/a/317550/55721