Пример #1
0
        private IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && !Ignore(wParam.ToInt32()))
            {
                var mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                var button    = GetButton(wParam.ToInt32());
                var state     = GetState(wParam.ToInt32());
                var info      = GetInfo(mouseData);

                if (callback(button, state, info))
                {
                    return((IntPtr)1);
                }
            }

            return(User32.CallNextHookEx(handle, nCode, wParam, lParam));
        }
Пример #2
0
        private static int KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            if ((Keys)vkCode == Keys.LControlKey || (Keys)vkCode == Keys.RControlKey)
            {
                if (wParam == (IntPtr)User32.WindowMessage.WM_KEYDOWN)
                {
                    UnclipCursor();
                }
                else if (wParam == (IntPtr)User32.WindowMessage.WM_KEYUP)
                {
                    ClipCursorToCurrentScreen();
                }
            }

            return(User32.CallNextHookEx(_keyboardHook.DangerousGetHandle(), nCode, wParam, lParam));
        }
Пример #3
0
                // Default filter function
                public int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
                {
                    if (code < 0)
                    {
                        return(User32.CallNextHookEx(m_hHook, code, wParam, lParam));
                    }

                    // Let clients determine what to do
                    HookEventArgs e = new HookEventArgs();

                    e.HookCode = code;
                    e.wParam   = wParam;
                    e.lParam   = lParam;
                    OnHookInvoked(e);

                    // Yield to the next hook in the chain
                    return(User32.CallNextHookEx(m_hHook, code, wParam, lParam));
                }
Пример #4
0
        public static IntPtr JournalRecordProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //throw new Exception("checking");
            if (nCode < 0)
            {
                return(User32.CallNextHookEx(journalHook, nCode, wParam, lParam));
            }

            EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));

            //do what you like to save the events
            using (StreamWriter sw = new StreamWriter("tempfile.txt", true))
            {
                sw.WriteLine(msg);
            }

            return(User32.CallNextHookEx(journalHook, nCode, wParam, lParam));
        }
Пример #5
0
        /// <summary>Hook callback to process <see cref="WM.WM_MOUSEMOVE" /> messages to highlight/un-highlight the close button on each tab.</summary>
        /// <param name="nCode">The message being received.</param>
        /// <param name="wParam">Additional information about the message.</param>
        /// <param name="lParam">Additional information about the message.</param>
        /// <returns>A zero value if the procedure processes the message; a nonzero value if the procedure ignores the message.</returns>
        protected IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            MouseEvent mouseEvent = new MouseEvent
            {
                nCode  = nCode,
                wParam = wParam,
                lParam = lParam
            };

            if (nCode >= 0 && (int)WM.WM_MOUSEMOVE == (int)wParam)
            {
                mouseEvent.MouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            }

            _mouseEvents.Add(mouseEvent);

            return(User32.CallNextHookEx(_hookId, nCode, wParam, lParam));
        }
Пример #6
0
        /// <summary>Hook callback to process <see cref="WM.WM_MOUSEMOVE" /> messages to move the thumbnail along with the cursor.</summary>
        /// <param name="nCode">The message being received.</param>
        /// <param name="wParam">Additional information about the message.</param>
        /// <param name="lParam">Additional information about the message.</param>
        /// <returns>A zero value if the procedure processes the message; a nonzero value if the procedure ignores the message.</returns>
        protected IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                switch ((WM)wParam)
                {
                case WM.WM_MOUSEMOVE:
                    MSLLHOOKSTRUCT hookStruct     = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                    Point          cursorPosition = new Point(hookStruct.pt.x, hookStruct.pt.y);

                    SetWindowPosition(cursorPosition);
                    {
                        var msg = new Message()
                        {
                            HWnd   = Handle,
                            LParam = lParam,
                            WParam = wParam,
                            Msg    = (int)WM.WM_MOUSEMOVE,
                        };
                        Invoke(new Action(() => m_messageFilter.PreFilterMessage(ref msg)));
                    }
                    break;

                case WM.WM_LBUTTONUP:
                {
                    var msg = new Message()
                    {
                        HWnd   = Handle,
                        LParam = lParam,
                        WParam = wParam,
                        Msg    = (int)WM.WM_LBUTTONUP,
                    };
                    Invoke(new Action(() => m_messageFilter.PreFilterMessage(ref msg)));
                }
                break;

                default:
                    break;
                }
            }

            return(User32.CallNextHookEx(m_hookId, nCode, wParam, lParam));
        }
Пример #7
0
        private IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // it was ok and someone listens to events
            if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
            {
                KBDLLHOOKSTRUCT MyKeyboardHookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
                // KeyDown
                if ((KeyDown != null) && (wParam == (IntPtr)WinUser.WM_KEYDOWN || wParam == (IntPtr)WinUser.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    this.KeyDown(this, e);
                }

                // KeyPress
                if ((KeyPress != null) && (wParam == (IntPtr)WinUser.WM_KEYDOWN))
                {
                    byte[] keyState = new byte[256];
                    User32.GetKeyboardState(keyState);

                    byte[] inBuffer = new byte[2];

                    if (User32.ToAscii(MyKeyboardHookStruct.vkCode,
                                       MyKeyboardHookStruct.scanCode,
                                       keyState,
                                       inBuffer,
                                       MyKeyboardHookStruct.flags == 1) == 1)
                    {
                        KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
                        KeyPress(this, e);
                    }
                }

                // KeyUp
                if ((KeyUp != null) && (wParam == (IntPtr)WinUser.WM_KEYUP || wParam == (IntPtr)WinUser.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    KeyUp(this, e);
                }
            }
            return(User32.CallNextHookEx(m_keyboardHook, nCode, wParam, lParam));
        }
Пример #8
0
        /// <summary>
        /// <inheritdoc cref="User32.WindowsHookDelegate"/>
        /// </summary>
        private static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var msg = (User32.WindowMessage)wParam;
                if (msg == User32.WindowMessage.WM_LBUTTONDOWN || msg == User32.WindowMessage.WM_RBUTTONDOWN)
                {
                    var input = Marshal.PtrToStructure <User32.MOUSEINPUT>(lParam);
                    MouseChanged?.Invoke(null, new MouseMessageArg
                    {
                        Msg = (int)msg,
                        X   = input.dx,
                        Y   = input.dy
                    });
                }
            }

            return(User32.CallNextHookEx(MouseHook.DangerousGetHandle(), nCode, wParam, lParam));
        }
Пример #9
0
 private int HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode >= 0 && wParam == (IntPtr)User32.WindowMessage.WM_KEYDOWN)
     {
         int vkCode = Marshal.ReadInt32(lParam);
         if (KeyCode.Count > 0)
         {
             if (KeyCode.Any(x => x == vkCode))
             {
                 Callback?.Invoke(vkCode);
             }
         }
         else
         {
             Callback?.Invoke(vkCode);
         }
     }
     return(User32.CallNextHookEx(handle, nCode, wParam, lParam));
 }
Пример #10
0
        private unsafe IntPtr LowLevelMouseAction(int nCode, IntPtr wParam, IntPtr lParam)
        {
            var state = (MouseState)(wParam.ToInt32() - 1); // fix for MouseMove 0x200 mask

            if ((MouseState.All & state) == state)
            {
                var p = Unsafe.Read <LowLevelMouseInputEvent>(lParam.ToPointer());

                var args = new GlobalMouseHookEventArgs(p, state);
                MousePressed?.Invoke(this, args);

                if (args.Handled)
                {
                    return((IntPtr)1);
                }
            }

            return(User32.CallNextHookEx(MouseHookHandle, nCode, wParam, lParam));
        }
Пример #11
0
        public IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var mouseStructure = Marshal.PtrToStructure <MouseLowLevelHookStruct>(lParam);
                var mouseMessage   = (MouseMessage)wParam;
                var exists         = MouseMessages.TryGetValue(mouseMessage, out var eventMethod);
                if (exists)
                {
                    eventMethod(mouseStructure.pt, mouseMessage);
                }
            }
            if (!StealthyMouseHook)
            {
                return(User32.CallNextHookEx(MouseHook.HookHandle, nCode, wParam, lParam));
            }

            return(IntPtr.Zero);
        }
Пример #12
0
 private IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 {
     if (nCode >= 0)
     {
         var keyboardStructure = Marshal.PtrToStructure <KeyboardLowLevelHookStruct>(lParam);
         var keyboardMessage   = (KeyboardMessage)wParam;
         var exists            = KeyboardMessages.TryGetValue((Keys)keyboardStructure.vkCode, out var eventMethod);
         if (exists)
         {
             Console.WriteLine(keyboardStructure.vkCode);
             eventMethod((Keys)keyboardStructure.vkCode, keyboardMessage);
         }
     }
     if (!StealthyKeyboardHook)
     {
         return(User32.CallNextHookEx(KeyboardHook.HookHandle, nCode, wParam, lParam));
     }
     return(IntPtr.Zero);
 }
Пример #13
0
        /// <summary>
        /// Called whenever the mouse moves. This routine leans entirely on the
        /// CheckJumpCursor() routine to see if there is any need to "mess with" the cursor
        /// position, to make it jump from one monitor to another.
        /// </summary>
        private IntPtr LLMouseHookCallback(int nCode, uint wParam, IntPtr lParam)
        {
            if (!(nCode < 0 || wParam != User32.WM_MOUSEMOVE || _updatingDisplaySettings))
            {
                var mouse = ((MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))).pt;

                // If we jump the cursor, then we return 1 here to tell the OS that we
                // have handled the message, so it doesn't call SetCursorPos() right
                // after we do, and "undo" our call to SetCursorPos().
                if (User32.GetCursorPos(out var cursor) && _screenSet.CheckJumpCursor(cursor, mouse, out var newCursor))
                {
                    newCursor.SetCursorPos();
                    return((IntPtr)1);
                }

                // Default is to let the OS handle the mouse events, when "return" does not happen in
                // if() clause above.
            }
            return(User32.CallNextHookEx(_llMouseHookhand, nCode, wParam, lParam));
        }
Пример #14
0
        private unsafe IntPtr LowLevelKeyboardAction(int nCode, IntPtr wParam, IntPtr lParam)
        {
            var wparamTyped = (KeyboardState)wParam.ToInt32();

            if ((KeyboardState.All & wparamTyped) == wparamTyped)
            {
                var p = Unsafe.Read <LowLevelKeyboardInputEvent>(lParam.ToPointer());

                if (KeyBinding.TryGet(p.VirtualCode, out _))
                {
                    var args = new GlobalKeyboardHookEventArgs(p, wparamTyped);
                    KeyboardPressed?.Invoke(this, args);

                    if (args.Handled)
                    {
                        return((IntPtr)1);
                    }
                }
            }

            return(User32.CallNextHookEx(KeyboardHookHandle, nCode, wParam, lParam));
        }
Пример #15
0
 private unsafe static IntPtr LLKeyboardCallback(int nCode, KeyboardButtonsState wParam, KeyBoardLowlevelHookStruct *lParam)
 {
     if (nCode > -1)
     {
         KeyBoardLowlevelHookStruct hookStruct = *lParam;
         VirtualKey key = hookStruct.VkCode;
         if (key == VirtualKey.LShift)
         {
             LShiftPress = (wParam == KeyboardButtonsState.WM_KEYDOWN);
         }
         if (key == VirtualKey.RShift)
         {
             RShiftPress = (wParam == KeyboardButtonsState.WM_KEYDOWN);
         }
         if (key == VirtualKey.CapsLock)
         {
             CapsPressed = (wParam == KeyboardButtonsState.WM_KEYDOWN);
         }
         string v = (LShiftPress || RShiftPress || CapsPressed) ? key.ToString() : key.ToString().ToLower();
         KeyboardEvent.Invoke(v, wParam, hookStruct);
     }
     return(User32.CallNextHookEx(KBHook, nCode, (IntPtr)wParam, (IntPtr)lParam));
 }
Пример #16
0
 private IntPtr ReceiveCallback(int code, IntPtr wParam, IntPtr lParam)
 {
     if (code == 0)
     {
         var hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
         var keys       = (Keys)hookStruct.vkCode;
         if ((hookStruct.flags & KBDLLHOOKSTRUCTFlags.LLKHF_UP) == KBDLLHOOKSTRUCTFlags.LLKHF_UP)
         {
             if (KeyUp != null)
             {
                 KeyUp(this, new KeyEventArgs(keys | Control.ModifierKeys));
             }
         }
         else
         {
             if (KeyDown != null)
             {
                 KeyDown(this, new KeyEventArgs(keys | Control.ModifierKeys));
             }
         }
     }
     return(User32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam));
 }
Пример #17
0
        public int HookProc(int code, IntPtr wParam, IntPtr lParam)
        {
            switch (code)
            {
            case 9:
                FocusChanged?.Invoke(this, new FocusChangeEventArgs(wParam, lParam));
                break;

            case 5:
                if (_insideActivateEvent.CanEnter)
                {
                    using (_insideActivateEvent.Enter())
                    {
                        //if (Activate != null)
                        //    Activate(this, new WindowActivateEventArgs(wParam));
                    }
                }
                break;
            }


            return(User32.CallNextHookEx(_windowHook, code, wParam, lParam));
        }
Пример #18
0
        private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(User32.CallNextHookEx(hookId, nCode, wParam, lParam));
            }



            if (wParam.ToUInt32() == (int)User32.KeyEvent.WM_KEYDOWN ||
                wParam.ToUInt32() == (int)User32.KeyEvent.WM_KEYUP ||
                wParam.ToUInt32() == (int)User32.KeyEvent.WM_SYSKEYDOWN ||
                wParam.ToUInt32() == (int)User32.KeyEvent.WM_SYSKEYUP)
            {
                bool handled = hookedKeyboardCallback.Invoke((User32.KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam));
                if (handled)
                {
                    return((IntPtr)1);
                }
            }

            return(User32.CallNextHookEx(hookId, nCode, wParam, lParam));
        }
 private int HookProc(int code, int wParam, ref User32.KeyboardHookStruct lParam)
 {
     if (code >= 0)
     {
         var key = (Keys)lParam.vkCode;
         if (HookedKeys.Contains(key))
         {
             var kea = new KeyEventArgs(key);
             if (wParam == User32.WM_KEYDOWN || wParam == User32.WM_SYSKEYDOWN)
             {
                 KeyDown?.Invoke(this, kea);
             }
             if (wParam == User32.WM_KEYUP || wParam == User32.WM_SYSKEYUP)
             {
                 KeyUp?.Invoke(this, kea);
             }
             if (kea.Handled)
             {
                 return(1);
             }
         }
     }
     return(User32.CallNextHookEx(_hhook, code, wParam, ref lParam));
 }
Пример #20
0
        private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var khs = (User32.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(User32.KeyboardHookStruct));
                if (khs.ExtraInfo != AppEventFlag && khs.VirtualKeyCode != PacketKey)
                {
                    switch (wParam)
                    {
                    case User32.WM_KEYDOWN:
                    case User32.WM_SYSKEYDOWN:
                        for (var i = Subscribed.Count - 1; i >= 0; i--)
                        {
                            if (Subscribed[i].RaiseKeyDown(khs.VirtualKeyCode))
                            {
                                return(-1);
                            }
                        }
                        break;

                    case User32.WM_KEYUP:
                    case User32.WM_SYSKEYUP:
                        for (var i = Subscribed.Count - 1; i >= 0; i--)
                        {
                            if (Subscribed[i].RaiseKeyUp(khs.VirtualKeyCode))
                            {
                                return(-1);
                            }
                        }
                        break;
                    }
                }
            }

            return(User32.CallNextHookEx(_hookHandle, nCode, wParam, lParam));
        }
Пример #21
0
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool handled = false;

            if (nCode >= 0)
            {
                if (((wParam == (IntPtr)WM.WM_KEYDOWN) || (wParam == (IntPtr)WM.WM_SYSKEYDOWN)))
                {
                    handled = OnKeyDown(lParam);
                }

                if (((wParam == (IntPtr)WM.WM_KEYUP) || (wParam == (IntPtr)WM.WM_SYSKEYUP)))
                {
                    handled = OnKeyUp(lParam);
                }
            }

            if (!handled)
            {
                return(User32.CallNextHookEx(hookId, nCode, wParam, lParam));
            }

            return(IntPtr.Zero);
        }
Пример #22
0
                private unsafe IntPtr MouseHookProc(User32.HC nCode, IntPtr wparam, IntPtr lparam)
                {
                    if (_isHooked && nCode == User32.HC.ACTION && lparam != IntPtr.Zero)
                    {
                        User32.MOUSEHOOKSTRUCT *mhs = (User32.MOUSEHOOKSTRUCT *)lparam;

                        try
                        {
                            if (ProcessMouseMessage(mhs->hWnd, unchecked ((int)(long)wparam), mhs->pt.X, mhs->pt.Y))
                            {
                                return((IntPtr)1);
                            }
                        }
                        catch (Exception ex)
                        {
                            _currentAdornerWindow.Capture = false;

                            if (ex != CheckoutException.Canceled)
                            {
                                _currentAdornerWindow._behaviorService.ShowError(ex);
                            }

                            if (ClientUtils.IsCriticalException(ex))
                            {
                                throw;
                            }
                        }
                        finally
                        {
                            _currentAdornerWindow = null;
                        }
                    }

                    Debug.Assert(_isHooked, "How did we get here when we are disposed?");
                    return(User32.CallNextHookEx(new HandleRef(this, _mouseHookHandle), nCode, wparam, lparam));
                }
Пример #23
0
        /// <summary>
        /// CBT callback called when a form is activated.
        /// If the newly activated form is modal and matches any registered names,
        /// invoke the appropriate hander.
        /// </summary>
        private int Callback_ModalListener(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code == HCBT_ACTIVATE)
            {
                // Some controls sends an HCBT_ACTIVATE when changed for example tabPages. We do not
                // want our handler to be called when a tabPage is changed. This is a problem in Modal
                // modal windows.
                if (!hwndList.Contains(wParam))
                {
                    hwndList.Add(wParam);
                    FindWindowNameAndInvokeHandler(wParam);
                }
            }
            if (code == HCBT_DESTROYWND)
            {
                // Need to remove the handle when the window is destroyed.
                if (hwndList.Contains(wParam))
                {
                    hwndList.Remove(wParam);
                }
            }

            return(User32.CallNextHookEx(handleToHook, code, wParam, lParam));
        }
Пример #24
0
        private IntPtr MsgHookCallBack(int nCode, IntPtr wParam, ref MSG lParam)
        {
            if (nCode < 0)
            {
                return(User32.CallNextHookEx(m_gmHookHandle, nCode, wParam, ref lParam));
            }

            if (true) //(wParam.ToInt32() == PM_NOREMOVE)
            {
                bool isMsgHandled = false;

                if (lParam.hwnd == this.m_windowHandle)
                {
                    Msgs wmMessage = (Msgs)lParam.message;

                    // check for the messages that should be handled (, and ignored).
                    if (hndldMsgType != Msgs.WM_NULL)
                    {
                        if (lParam.wParam == hndldMsgWP)
                        {
                            switch (hndldMsgType)
                            {
                            case Msgs.WM_KEYUP:
                            case Msgs.WM_SYSKEYUP:
                                hndldMsgType = Msgs.WM_NULL;
                                break;

                            case Msgs.WM_KEYDOWN:
                            case Msgs.WM_SYSKEYDOWN:
                                hndldMsgType = Msgs.WM_CHAR;
                                break;

                            case Msgs.WM_CHAR:
                            case Msgs.WM_SYSCHAR:
                                hndldMsgType = Msgs.WM_KEYUP;
                                break;

                            default:
                                hndldMsgType = Msgs.WM_NULL;
                                break;
                            }

                            lParam.message = (uint)Msgs.WM_NULL;
                            lParam.wParam  = 0;
                            lParam.lParam  = 0;
                            isMsgHandled   = true;
                        }
                        else
                        {
                            hndldMsgType = Msgs.WM_NULL;
                        }
                    }

                    if (!isMsgHandled &&
                        !(lParam.message == prevMsgType && lParam.time == prevMsgTime))
                    {
                        WindowListenerEventArgs eventArgs = new WindowListenerEventArgs();
                        eventArgs.handled = false;
                        eventArgs.message = (int)lParam.message;
                        eventArgs.lparam  = lParam.lParam;
                        eventArgs.wparam  = lParam.wParam;

                        prevMsgType = (int)lParam.message;
                        prevMsgTime = lParam.time;

                        switch (wmMessage)
                        {
                        case Msgs.WM_KEYUP:
                        case Msgs.WM_SYSKEYUP:
                            if (KeyUp != null)
                            {
                                KeyUp(this, eventArgs);
                            }
                            break;

                        case Msgs.WM_KEYDOWN:
                        case Msgs.WM_SYSKEYDOWN:
                            if (KeyDown != null)
                            {
                                KeyDown(this, eventArgs);
                            }
                            break;

                        case Msgs.WM_CHAR:
                        case Msgs.WM_SYSCHAR:
                            if (KeyPressed != null)
                            {
                                KeyPressed(this, eventArgs);
                            }
                            break;

                        default:
                            if (OtherMessage != null)
                            {
                                OtherMessage(this, eventArgs);
                            }
                            break;
                        }

                        if (eventArgs.handled)
                        {
                            hndldMsgTime = lParam.time;
                            hndldMsgType = wmMessage;
                            hndldMsgWP   = lParam.time;

                            lParam.message = (uint)Msgs.WM_NULL;
                            lParam.lParam  = 0;
                            lParam.wParam  = 0;
                        }
                    }

                    if (!isMsgHandled && CheckIgnoreCodes)
                    {
                        if (wmMessage == Msgs.WM_KEYDOWN || wmMessage == Msgs.WM_KEYUP || wmMessage == Msgs.WM_CHAR ||
                            wmMessage == Msgs.WM_SYSKEYDOWN || wmMessage == Msgs.WM_SYSKEYUP || wmMessage == Msgs.WM_SYSCHAR)
                        {
                            if (m_setKeyboardIgnoreCodes.Contains(lParam.wParam))
                            {
                                lParam.message = (uint)Msgs.WM_NULL;
                                lParam.lParam  = 0;
                                lParam.wParam  = 0;
                            }
                        }
                    }
                }

                if (lParam.message == (uint)Msgs.WM_NULL && lParam.lParam == 0 && lParam.wParam == 0)
                {
                    return((IntPtr)1);
                }
            }

            return(User32.CallNextHookEx(m_gmHookHandle, nCode, wParam, ref lParam));
        }
Пример #25
0
        private IntPtr CwpHookCallBack(int nCode, IntPtr wParam, ref CWPSTRUCT lParam)
        {
            if (nCode < 0)
            {
                return(User32.CallNextHookEx(m_gmHookHandle, nCode, wParam, ref lParam));
            }

            if (true) //(wParam.ToInt32() == PM_NOREMOVE)
            {
                if (lParam.hwnd == this.m_windowHandle)
                {
                    WindowListenerEventArgs eventArgs = new WindowListenerEventArgs();
                    eventArgs.handled = false;
                    eventArgs.message = (int)lParam.message;
                    eventArgs.lparam  = lParam.lParam;
                    eventArgs.wparam  = lParam.wParam;

                    Msgs wmMessage = (Msgs)lParam.message;

                    switch (wmMessage)
                    {
                    case Msgs.WM_KILLFOCUS:
                        if (LostFocus != null)
                        {
                            LostFocus(this, eventArgs);
                        }
                        break;

                    case Msgs.WM_SETFOCUS:
                        if (GetFocus != null)
                        {
                            GetFocus(this, eventArgs);
                        }
                        break;

                    case Msgs.WM_WINDOWPOSCHANGING:
                        if (PositionChanging != null)
                        {
                            PositionChanging(this, eventArgs);
                        }
                        break;

                    case Msgs.WM_PAINT:
                        if (this.Paint != null)
                        {
                            Paint(this, eventArgs);
                        }
                        break;

                    default:
                        if (OtherMessage != null)
                        {
                            OtherMessage(this, eventArgs);
                        }
                        break;
                    }

                    if (eventArgs.handled)
                    {
                        lParam.message = (uint)Msgs.WM_NULL;
                        lParam.lParam  = 0;
                        lParam.wParam  = 0;

                        return((IntPtr)1);
                    }
                }
            }

            return(User32.CallNextHookEx(m_cwpHookHandle, nCode, wParam, ref lParam));
        }
Пример #26
0
 private int test(int c, IntPtr a, IntPtr b)
 {
     return(User32.CallNextHookEx(new IntPtr(hKeyboardHook), c, a, b));
 }
Пример #27
0
 private int WindowsHookProc(int nCode, IntPtr wParam, IntPtr lParam)
 {
     //Something...
     return(User32.CallNextHookEx(new IntPtr(wHookId), nCode, wParam, lParam));
 }
Пример #28
0
        /// <summary>
        /// A callback function which will be called every Time a mouse activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //Marshall the data from callback.
                MSLLHOOKSTRUCT mouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

                //detect button clicked
                MouseButtons button     = MouseButtons.None;
                short        mouseDelta = 0;
                int          clickCount = 0;
                bool         mouseDown  = false;
                bool         mouseUp    = false;
                WM           command    = (WM)wParam;
                switch (command)
                {
                case WM.WM_LBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WM.WM_LBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Left;
                    clickCount = 1;
                    break;

                case WM.WM_LBUTTONDBLCLK:
                    button     = MouseButtons.Left;
                    clickCount = 2;
                    break;

                case WM.WM_RBUTTONDOWN:
                    mouseDown  = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WM.WM_RBUTTONUP:
                    mouseUp    = true;
                    button     = MouseButtons.Right;
                    clickCount = 1;
                    break;

                case WM.WM_RBUTTONDBLCLK:
                    button     = MouseButtons.Right;
                    clickCount = 2;
                    break;

                case WM.WM_MOUSEWHEEL:
                    //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta.
                    //One wheel click is defined as WHEEL_DELTA, which is 120.
                    //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff);

                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                    //and the low-order word is reserved. This value can be one or more of the following values.
                    //Otherwise, MouseData is not used.
                    break;
                }

                //generate event
                MouseEventExtArgs e = new MouseEventExtArgs(
                    button,
                    clickCount,
                    mouseHookStruct.Point.X,
                    mouseHookStruct.Point.Y,
                    mouseDelta);

                //Mouse up
                if (s_MouseUp != null && mouseUp)
                {
                    s_MouseUp.Invoke(null, e);
                }

                //Mouse down
                if (s_MouseDown != null && mouseDown)
                {
                    s_MouseDown.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClick != null && clickCount > 0)
                {
                    s_MouseClick.Invoke(null, e);
                }

                //If someone listens to click and a click is heppened
                if (s_MouseClickExt != null && clickCount > 0)
                {
                    s_MouseClickExt.Invoke(null, e);
                }

                //If someone listens to double click and a click is heppened
                if (s_MouseDoubleClick != null && clickCount == 2)
                {
                    s_MouseDoubleClick.Invoke(null, e);
                }

                //Wheel was moved
                if (s_MouseWheel != null && mouseDelta != 0)
                {
                    s_MouseWheel.Invoke(null, e);
                }

                //If someone listens to move and there was a change in coordinates raise move event
                if ((s_MouseMove != null || s_MouseMoveExt != null) && (m_OldX != mouseHookStruct.Point.X || m_OldY != mouseHookStruct.Point.Y))
                {
                    m_OldX = mouseHookStruct.Point.X;
                    m_OldY = mouseHookStruct.Point.Y;
                    if (s_MouseMove != null)
                    {
                        s_MouseMove.Invoke(null, e);
                    }

                    if (s_MouseMoveExt != null)
                    {
                        s_MouseMoveExt.Invoke(null, e);
                    }
                }

                if (e.Handled)
                {
                    return(-1);
                }
            }

            //call next hook
            return(User32.CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam));
        }
Пример #29
0
        /// <summary>
        /// A callback function which will be called every Time a keyboard activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private static int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            bool handled = false;

            if (nCode >= 0)
            {
                WM command = (WM)wParam;
                //read structure KeyboardHookStruct at lParam
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                //raise KeyDown
                if (s_KeyDown != null && (command == WM.WM_KEYDOWN || command == WM.WM_SYSKEYDOWN))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    s_KeyDown.Invoke(null, e);
                    handled = e.Handled;
                }

                // raise KeyPress
                if (s_KeyPress != null && command == WM.WM_KEYDOWN)
                {
                    bool isDownShift    = ((User32.GetKeyState((int)VK.VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    bool isDownCapslock = (User32.GetKeyState((int)VK.VK_CAPITAL) != 0 ? true : false);

                    byte[] keyState = new byte[256];
                    User32.GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (User32.ToAscii(MyKeyboardHookStruct.VirtualKeyCode,
                                       MyKeyboardHookStruct.ScanCode,
                                       keyState,
                                       inBuffer,
                                       MyKeyboardHookStruct.Flags) == 1)
                    {
                        char key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }
                        KeyPressEventArgs e = new KeyPressEventArgs(key);
                        s_KeyPress.Invoke(null, e);
                        handled = handled || e.Handled;
                    }
                }

                // raise KeyUp
                if (s_KeyUp != null && (command == WM.WM_KEYUP || command == WM.WM_SYSKEYUP))
                {
                    Keys         keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode;
                    KeyEventArgs e       = new KeyEventArgs(keyData);
                    s_KeyUp.Invoke(null, e);
                    handled = handled || e.Handled;
                }
            }

            //if event handled in application do not handoff to other listeners
            if (handled)
            {
                return(-1);
            }

            //forward to other application
            return(User32.CallNextHookEx(s_KeyboardHookHandle, nCode, wParam, lParam));
        }
Пример #30
0
        /// <summary>
        /// Processes shortcut keys.
        /// </summary>
        /// <param name="nCode">Code indicating if we should process this message.</param>
        /// <param name="wParam"><see cref="WM"/> enumeration value which is the message that's being passed to us.</param>
        /// <param name="lParam">Virtual key code of the key being pressed.</param>
        /// <returns>The result of the next hook in the queue (<see cref="User32.CallNextHookEx"/>).</returns>
        protected IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // Only process the key press if our window is active
            if (nCode >= 0 && User32.GetActiveWindow() == Handle)
            {
                // Get the key that was pressed
                Key key = KeyInterop.KeyFromVirtualKey(Marshal.ReadInt32(lParam));

                switch ((WM)wParam.ToInt32())
                {
                case WM.WM_KEYDOWN:
                case WM.WM_SYSKEYDOWN:
                    switch (key)
                    {
                    case Key.RightCtrl:
                    case Key.LeftCtrl:
                        _ctrlDown = true;
                        break;

                    case Key.RightShift:
                    case Key.LeftShift:
                        _shiftDown = true;
                        break;

                    // Ctrl+T creates a new tab
                    case Key.T:
                        if (_ctrlDown)
                        {
                            AddNewTab();
                        }

                        break;

                    // Ctrl+Tab cycles forward in the tab list, while Ctrl+Shift+Tab cycles backward
                    case Key.Tab:
                        if (Tabs.Count > 1)
                        {
                            if (_ctrlDown && _shiftDown)
                            {
                                if (SelectedTabIndex == 0)
                                {
                                    SelectedTabIndex = Tabs.Count - 1;
                                }

                                else
                                {
                                    SelectedTabIndex--;
                                }
                            }

                            else if (_ctrlDown)
                            {
                                if (SelectedTabIndex == Tabs.Count - 1)
                                {
                                    SelectedTabIndex = 0;
                                }

                                else
                                {
                                    SelectedTabIndex++;
                                }
                            }
                        }

                        break;
                    }

                    break;

                case WM.WM_KEYUP:
                case WM.WM_SYSKEYUP:
                    switch (key)
                    {
                    case Key.RightCtrl:
                    case Key.LeftCtrl:
                        _ctrlDown = false;
                        break;

                    case Key.RightShift:
                    case Key.LeftShift:
                        _shiftDown = false;
                        break;
                    }

                    break;
                }
            }

            // Call the next hook in the queue
            return(User32.CallNextHookEx(_hookId, nCode, wParam, lParam));
        }