qq_32254597
飞了啊
采纳率0%
2019-03-12 09:22 阅读 1.3k

C# 键盘钩子怎么获取两个任意的组合键!!!!!!!

5

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));

        if ((int)Control.ModifierKeys == (int)Keys.PageDown+ (int)Keys.PageUp)  // 截获F8
        {
            //Debug.Log("快捷键已拦截!不能保存!");
            //return 1;
        }
        if (kbh.vkCode == (int)Keys.A )
        {
            //Debug.Log("A+B");   
        }
    }
return CallNextHookEx(_hookID, nCode, wParam, lParam);

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • qq_16427421 刘超峰 2019-03-27 07:49
    //问题:C# 键盘钩子怎么获取两个任意的组合键!!!!!!!
            //思路:截获按键按下消息(WM_KEYDOWN、WM_SYSKEYDOWN)和按键释放消息(WM_KEYUP、WM_SYSKEYUP)
            //      用数组记录按下/释放状态,由于只记录按下/释放,遂用bool类型数组,数组大小为256
            //      在WM_KEYDOWN/WM_SYSKEYDOWN下遍历数组,把所有值为Ture的索引转换为虚拟键代码
    
            //用于记录键状态
            bool[] keyDown = new bool[256];
            //用于记录被按下的键
            List<Keys> pressedKeys = new List<Keys>();
            private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                //WM_KEYUP||WM_SYSKEYUP
                //......
                keyDown[vkCode] = false;
                //......
                //WM_KEYDOWN||WM_SYSKEYDOWN
                //......
                keyDown[vkCode] = true;
                int index = -1;
                index = Array.IndexOf(keyDown, true, index + 1);
                while (index != -1)
                {
                    pressedKeys.Add((Keys)index);
                    index = Array.IndexOf(keyDown, true, index + 1);
                };
                //此时 pressedKeys 里的值为当前所有被按下的键。
                //......
            }
    
    点赞 评论 复制链接分享

相关推荐