Exemplo n.º 1
0
 public void Unhook()
 {
     if (intPtrHook != IntPtr.Zero)
     {
         CtkUser32Lib.UnhookWindowsHookEx(intPtrHook);
         intPtrHook = IntPtr.Zero;
     }
 }
        public static void SendMouseInput(CtkEnumMouseFlag mouseFlag)
        {
            CtkMdlInput mouseInput = new CtkMdlInput();

            mouseInput.dwType         = 0;
            mouseInput.mi             = new CtkMdlHookMouseStruct();
            mouseInput.mi.dwExtraInfo = IntPtr.Zero;
            mouseInput.mi.dx          = 0;
            mouseInput.mi.dy          = 0;
            mouseInput.mi.time        = 0;
            mouseInput.mi.mouseData   = 0;
            mouseInput.mi.dwFlags     = mouseFlag;

            CtkUser32Lib.SendInput(1, ref mouseInput, System.Runtime.InteropServices.Marshal.SizeOf(typeof(CtkMdlInput)));
        }
        public static void KeyUp(short vk)
        {
            CtkMdlInput input = new CtkMdlInput();

            input.dwType = CtkMdlInputType.Keyboard;


            input.ki             = new CtkMdlHookKeyboardStruct();
            input.ki.wVk         = vk;
            input.ki.dwFlags     = CtkEnumKeyboardFlag.KEYUP;
            input.ki.wScan       = 0;
            input.ki.time        = 0;
            input.ki.dwExtraInfo = IntPtr.Zero;

            CtkUser32Lib.SendInput(1, ref input, System.Runtime.InteropServices.Marshal.SizeOf(typeof(CtkMdlInput)));
        }
Exemplo n.º 4
0
        public void Hook()
        {
            //Hook Keyboard
            Unhook();
            if (intPtrHook == IntPtr.Zero)
            {
                hookProc   = new CtkUser32Lib.HookProc(HookProcCallback);
                intPtrHook = CtkUser32Lib.SetWindowsHookEx(CtkEnumHookType.WH_KEYBOARD_LL,
                                                           hookProc,
                                                           IntPtr.Zero,
                                                           0);

                if (intPtrHook == IntPtr.Zero)
                {
                    throw new CtkException("WinApi Error-" + System.Runtime.InteropServices.Marshal.GetLastWin32Error());
                }
            }
        }
Exemplo n.º 5
0
        protected int HookProcCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                try { this.OnHookCallback(new CtkWinApiEventArgsHookCallback()
                    {
                        nCode = nCode, wParam = wParam, lParam = lParam
                    }); }
                catch (Exception ex) { ThreadPool.QueueUserWorkItem(delegate { this.OnHookCallbackException(new CtkWinApiEventArgsException()
                        {
                            exception = ex
                        }); }); }

                try
                {
                    int          vkCode  = System.Runtime.InteropServices.Marshal.ReadInt32(lParam);
                    CtkEnumConst kbInput = (CtkEnumConst)wParam;

                    if (kbInput == CtkEnumConst.WM_KEYDOWN || kbInput == CtkEnumConst.WM_SYSKEYDOWN)
                    {
                        keepKeys[vkCode] = true;
                    }
                    if (kbInput == CtkEnumConst.WM_KEYUP || kbInput == CtkEnumConst.WM_SYSKEYUP)
                    {
                        keepKeys[vkCode] = false;
                    }
                }
                catch (Exception ex)
                {
                    //給背景執行緒處理, 再出Exception也與原執行緒無關, 可以正常工作
                    ThreadPool.QueueUserWorkItem(delegate { this.OnHookCallbackException(new CtkWinApiEventArgsException()
                        {
                            exception = ex
                        }); });
                }
            }
            return(CtkUser32Lib.CallNextHookEx(intPtrHook, nCode, wParam, lParam));
        }
        int HookProcCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            EventArgsMouse ehargs = null;

            if (nCode >= 0)
            {
                try
                {
                    var hookType        = (CtkEnumConst)wParam;
                    var mouseHookStruct = (CtkMdlHookMouseStruct)Marshal.PtrToStructure(lParam, typeof(CtkMdlHookMouseStruct));

                    short mouseDelta = 0;

                    if (hookType == CtkEnumConst.WM_MOUSEWHEEL)
                    {
                        mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);
                    }

                    ehargs = new EventArgsMouse(hookType, mouseHookStruct.dx, mouseHookStruct.dy, mouseDelta);

                    if (hookType == CtkEnumConst.WM_MOUSEWHEEL)
                    {
                        this.OnMouseWheel(ehargs);
                    }

                    else if (hookType == CtkEnumConst.WM_LBUTTONUP || hookType == CtkEnumConst.WM_RBUTTONUP || hookType == CtkEnumConst.WM_MBUTTONUP)
                    {
                        this.OnMouseUp(ehargs);
                        if (mouseHookStruct.dx == m_LastBTDownX && mouseHookStruct.dy == m_LastBTDownY)
                        {
                            this.OnMouseClick(ehargs);
                        }
                    }
                    else if (hookType == CtkEnumConst.WM_LBUTTONDOWN || hookType == CtkEnumConst.WM_RBUTTONDOWN || hookType == CtkEnumConst.WM_MBUTTONDOWN)
                    {
                        m_LastBTDownX = mouseHookStruct.dx;
                        m_LastBTDownY = mouseHookStruct.dy;
                        this.OnMouseDown(ehargs);
                    }
                    else if (m_OldX != mouseHookStruct.dx || m_OldY != mouseHookStruct.dy)
                    {
                        m_OldX = mouseHookStruct.dx;
                        m_OldY = mouseHookStruct.dy;
                        this.OnMouseMove(ehargs);
                    }
                }
                catch (Exception ex)
                {
                    //給背景執行緒處理, 再出Exception也與原執行緒無關, 可以正常工作
                    ThreadPool.QueueUserWorkItem(delegate { this.OnHookCallbackException(new CtkWinApiEventArgsException()
                        {
                            exception = ex
                        }); });
                }
            }

            if (Monopolize || (ehargs != null && ehargs.Handled))
            {
                return(-1);
            }


            return(CtkUser32Lib.CallNextHookEx(intPtrHook, nCode, wParam, lParam));
        }