如何使用LowLevelKeyboardHook挂钩WIN + Tab键 [英] How to hook Win + Tab using LowLevelKeyboardHook

查看:184
本文介绍了如何使用LowLevelKeyboardHook挂钩WIN + Tab键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几句话:堵<大骨节病>赢后<大骨节病>赢 + <大骨节病>标签使Windows认为<大骨节病>赢仍下跌,所以然后按<大骨节病>取值与<大骨节病>赢键为例子将打开搜索的魅力,而不是仅仅在用户按下S型... <大骨节病>赢< /骨节病>一次。的的阻止它意味着Windows开始菜单中会显示出来。我在一个难题!






我没有问题挂钩到使用快捷键<大骨节病>替代 + <大骨节病>标签 LowLevelKeyboardHook 或> <大骨节病>赢 + <大骨节病>一些Ubounded键使用 RegisterHotKey 。这个问题只发生使用 LowLevelKeyboardHook 在<大骨节病>赢键。



在下面的例子中当检测到<大骨节病>赢 + <大骨节病>标签组合当我接手<大骨节病>赢事件。这将导致使每以下按键行为,如果在<大骨节病>赢键仍下降。

 私有静态IntPtr的HookCallback(INT nCode,wParam中的IntPtr,IntPtr的lParam的)
{
如果(nCode = HC_ACTION!)
返回CallNextHookEx方法(_hookID,nCode,wParam中,lParam的);

VAR的密钥信息=(Kbdllhookstruct)Marshal.PtrToStructure(lParam的,typeof运算(Kbdllhookstruct));

如果(keyInfo.VkCode == VK_LWIN)
{
如果(wParam中==(IntPtr的)WM_KEYDOWN){
_isWinDown = TRUE;
}其他{
_isWinDown = FALSE;

如果(_isWinTabDetected){
_isWinTabDetected = FALSE;
回报(IntPtr的)1;
}
}
}
,否则如果(keyInfo.VkCode == VK_TAB&放大器;&安培; _isWinDown){
_isWinTabDetected = TRUE;

如果(wParam中==(IntPtr的)WM_KEYDOWN){
回报(IntPtr的)1;
}其他{
_isWinTabDetected = TRUE;
Console.WriteLine(WIN + TAB按下);
回报(IntPtr的)1;
}
}

返回CallNextHookEx方法(_hookID,nCode,wParam中,lParam的);
}
}
}

您可以找到完整的代码这里(请注意,就应更换你的的Program.cs 在一个空的WinForms项目运行)的 https://gist.github.com/christianrondeau/bdd03a3dc32a7a718d62 - 按<大骨节病>赢 + <大骨节病>标签表格标题应该每次按下快捷键时更新。





请注意该的意图的挂钩到这个特定组合的是提供一个<大骨节病>替代 +无需更换<大骨节病>标签替代<大骨节病>替代 + <大骨节病>标签本身。答案提供的能力,使用发射自定义代码<大骨节病>赢 + <大骨节病>标签也将被接受。





下面是我的想法,为此,我找不到文档。所有将可能回答我的问题成功。




  • 告诉Windows取消<大骨节病>赢时没有真正触发它

  • 防止Windows一次启动开始菜单

  • 在Windows的直接挂钩<大骨节病>赢 +事件,而不是手动挂钩进入击键(这将是的目前的,如果存在我的第一选择)


解决方案

这似乎做的正是你想要的(如果你想忽略RWIN)的东西。



请体谅,当你的应用程序失去焦点注销此KB挂钩!

 函数[DllImport(user32.dll中)] 
静态外部短GetAsyncKeyState(System.Windows.Forms的。键v键);

私有静态的IntPtr HookCallback(INT nCode,wParam中的IntPtr,IntPtr的lParam的)
{
如果(nCode == HC_ACTION)
{
VAR密钥信息= (Kbdllhookstruct)Marshal.PtrToStructure(lParam的,typeof运算(Kbdllhookstruct));
如果((INT)的wParam == WM_KEYDOWN
和;&安培; keyInfo.VkCode == VK_TAB
和;及(GetAsyncKeyState(Keys.LWin)℃下|| GetAsyncKeyState(钥匙.RWin)小于0))
{
_mainForm.Text =WIN + Tab键被按下+(++ _ winTabPressCounter)+时代;
回报(IntPtr的)1;
}
}

返回CallNextHookEx方法(_hookID,nCode,wParam中,lParam的);
}



我发现这个技术之前试过几件事情。
这个职位是最有帮助的 http://stackoverflow.com/a/317550/55721


In a few words: blocking Win up after Win + Tab makes Windows think Win is still down, so then pressing S with the Win key up for example will open the search charm rather than just type "s"... until the user presses Win again. Not blocking it means the Windows Start menu will show up. I'm in a conundrum!


I have no trouble hooking into shortcuts using Alt + Tab using LowLevelKeyboardHook, or Win + Some Ubounded Key using RegisterHotKey. The problem happens only with the Win key using LowLevelKeyboardHook.

In the example below, I'm taking over the Win up event when the Win + Tab combination is detected. This results in making every following keystrokes behave as if the Win key was still down.

        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, nCode, wParam, lParam);
        }
    }
}

You can find the complete code here (note that it should replace your Program.cs in an empty WinForms project to run): https://gist.github.com/christianrondeau/bdd03a3dc32a7a718d62 - press Win + Tab and the Form title should update each time the shortcut is pressed.

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.

Here are my ideas, for which I could not find documentation. All would potentially answer my question successfully.

  • Tell Windows to "cancel" the Win up without actually triggering it
  • Prevent Windows from launching the Start menu once
  • Hook directly in the Windows' Win + event rather than manually hooking into the keystrokes (This would be by far my first choice if that exists)

解决方案

This appears to do exactly what you want (omit RWin if you wish).

Please be considerate and unregister this KB hook when your app loses focus!

    [DllImport("user32.dll")]
    static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);

    private static IntPtr HookCallback(int nCode, IntPtr wParam, 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, nCode, wParam, lParam);
    }

I tried several things before discovering this technique. This post was the most helpful http://stackoverflow.com/a/317550/55721

这篇关于如何使用LowLevelKeyboardHook挂钩WIN + Tab键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆