示例#1
0
 /// <summary>
 /// 禁用键盘挂钩。
 /// </summary>
 /// <returns>True如果禁用正确。</returns>
 public static bool Disable()
 {
     if (Enabled == true)
     {
         try
         {
             Hooks.UnhookWindowsHookEx(hHook);
             Enabled = false;
             return(true);
         }
         catch
         {
             Enabled = true;
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
示例#2
0
 /// <summary>
 /// 启动键盘钩子。
 /// </summary>
 /// <returns>True如果没有例外。</returns>
 public static bool Enable()
 {
     if (Enabled == false)
     {
         try
         {
             using (Process curProcess = Process.GetCurrentProcess())
                 using (ProcessModule curModule = curProcess.MainModule)
                     hHook = Hooks.SetWindowsHookEx((int)Hooks.HookType.WH_KEYBOARD_LL, hookproc, Hooks.GetModuleHandle(curModule.ModuleName), 0);
             Enabled = true;
             return(true);
         }
         catch
         {
             Enabled = false;
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
示例#3
0
        private static IntPtr Filter(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool result = true;

            if (nCode >= 0)
            {
                if (wParam == (IntPtr)Hooks.WM_KEYDOWN ||
                    wParam == (IntPtr)Hooks.WM_SYSKEYDOWN)
                {
                    int vkCode = Marshal.ReadInt32(lParam);
                    if ((Keys)vkCode == Keys.LControlKey ||
                        (Keys)vkCode == Keys.RControlKey)
                    {
                        Control = true;
                    }
                    else if ((Keys)vkCode == Keys.LShiftKey ||
                             (Keys)vkCode == Keys.RShiftKey)
                    {
                        Shift = true;
                    }
                    else if ((Keys)vkCode == Keys.RMenu ||
                             (Keys)vkCode == Keys.LMenu)
                    {
                        Alt = true;
                    }
                    else if ((Keys)vkCode == Keys.RWin ||
                             (Keys)vkCode == Keys.LWin)
                    {
                        Win = true;
                    }
                    else
                    {
                        result = OnKeyDown((Keys)vkCode);
                    }
                }
                else if (wParam == (IntPtr)Hooks.WM_KEYUP ||
                         wParam == (IntPtr)Hooks.WM_SYSKEYUP)
                {
                    int vkCode = Marshal.ReadInt32(lParam);
                    if ((Keys)vkCode == Keys.LControlKey ||
                        (Keys)vkCode == Keys.RControlKey)
                    {
                        Control = false;
                    }
                    else if ((Keys)vkCode == Keys.LShiftKey ||
                             (Keys)vkCode == Keys.RShiftKey)
                    {
                        Shift = false;
                    }
                    else if ((Keys)vkCode == Keys.RMenu ||
                             (Keys)vkCode == Keys.LMenu)
                    {
                        Alt = false;
                    }
                    else if ((Keys)vkCode == Keys.RWin ||
                             (Keys)vkCode == Keys.LWin)
                    {
                        Win = false;
                    }
                    else
                    {
                        result = OnKeyUp((Keys)vkCode);
                    }
                }
            }

            return(result ? Hooks.CallNextHookEx(hHook, nCode, wParam, lParam) : new IntPtr(1));
        }
示例#4
0
        private static IntPtr Filter(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool result = true;

            if (nCode >= 0)
            {
                Hooks.MouseHookStruct info = (Hooks.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Hooks.MouseHookStruct));
                switch ((int)wParam)
                {
                case Hooks.WM_LBUTTONDOWN:
                    result = OnMouseDown(MouseButtons.Left);
                    break;

                case Hooks.WM_LBUTTONUP:
                    result = OnMouseUp(MouseButtons.Left);
                    break;

                case Hooks.WM_RBUTTONDOWN:
                    result = OnMouseDown(MouseButtons.Right);
                    break;

                case Hooks.WM_RBUTTONUP:
                    result = OnMouseUp(MouseButtons.Right);
                    break;

                case Hooks.WM_MBUTTONDOWN:
                    result = OnMouseDown(MouseButtons.Middle);
                    break;

                case Hooks.WM_MBUTTONUP:
                    result = OnMouseUp(MouseButtons.Middle);
                    break;

                case Hooks.WM_XBUTTONDOWN:
                    if (info.Data >> 16 == Hooks.XBUTTON1)
                    {
                        result = OnMouseDown(MouseButtons.XButton1);
                    }
                    else if (info.Data >> 16 == Hooks.XBUTTON2)
                    {
                        result = OnMouseDown(MouseButtons.XButton2);
                    }
                    break;

                case Hooks.WM_XBUTTONUP:
                    if (info.Data >> 16 == Hooks.XBUTTON1)
                    {
                        result = OnMouseUp(MouseButtons.XButton1);
                    }
                    else if (info.Data >> 16 == Hooks.XBUTTON2)
                    {
                        result = OnMouseUp(MouseButtons.XButton2);
                    }
                    break;

                case Hooks.WM_MOUSEMOVE:
                    result = OnMouseMove(new Point(info.Point.X, info.Point.Y));
                    break;

                case Hooks.WM_MOUSEWHEEL:
                    result = OnMouseWheel((info.Data >> 16) & 0xffff);
                    break;
                }
            }

            return(result ? Hooks.CallNextHookEx(hHook, nCode, wParam, lParam) : new IntPtr(1));
        }