Esempio n. 1
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));
        }