Exemple #1
0
        /// <summary>
        /// Serializes the events
        /// </summary>
        /// <param name="eventClass">Event class</param>
        /// <param name="type">Type of event</param>
        /// <param name="args">KeyHookEventArgs or MouseHookEventArgs object</param>
        /// <param name="pressedKeys">Upto 3 consecutively pressed keys</param>
        /// <param name="pressedChar">Pressed character</param>
        /// <returns>Data bytes</returns>
        public static byte[] GetEncodedData(EventClass eventClass, EventType type, HookEventArgs args, int[] pressedKeys = null, char pressedChar = '\0')
        {
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                ms.WriteByte(GetHeaderByte(eventClass, type, args));
                if (eventClass == EventClass.MouseEvent)
                {
                    MouseHookEventArgs me = args as MouseHookEventArgs;
                    ms.Write(BitConverter.GetBytes((ushort)me.Location.X), 0, 2);
                    ms.Write(BitConverter.GetBytes((ushort)me.Location.Y), 0, 2);
                    //ms.WriteByte((byte)me.Location.X);
                    //ms.WriteByte((byte)me.Location.Y);
                    //System.Windows.Forms.MouseButtons.Right;

                    ms.WriteByte((byte)((int)me.Button >> 20));
                    byte extra = 0;
                    switch (type)
                    {
                    case EventType.MouseClick:
                    case EventType.MouseDoubleClick:
                        extra = (byte)me.ClickCount;
                        break;

                    case EventType.MouseWheel:
                        extra = (byte)Math.Abs(me.ScrollAmount);
                        if (me.ScrollAmount < 0)
                        {
                            extra |= 0x80;
                        }
                        break;
                    }
                    ms.WriteByte(extra);
                }
                else
                {
                    KeyHookEventArgs ke    = args as KeyHookEventArgs;
                    byte[]           pKeys = { 0, 0, 0 };
                    if (pressedKeys == null || pressedKeys.Length == 0)
                    {
                        throw new ArgumentException("Atleast one key should be in pressed keys.");
                    }
                    int len = Math.Min(3, pressedKeys.Length);
                    for (int i = 0; i < len; i++)
                    {
                        pKeys[i] = (byte)pressedKeys[i];
                    }

                    ms.Write(pKeys, 0, 3);
                    ms.WriteByte((byte)pressedChar);
                }

                data = ms.ToArray();
            }
            return(data);
        }
Exemple #2
0
        /// <summary>
        /// Callback for LL MouseHook
        /// </summary>        
        private IntPtr MouseCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
                return USER32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam);

            // Else Process

            bool handled = false;
            USER32.MSLLHOOKSTRUCT mse = (USER32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(USER32.MSLLHOOKSTRUCT));
            int wP = wParam.ToInt32();

            MouseButtons button = MouseButtons.None;
            short mouseDelta = 0;
            int clickCount = 0;
            bool mouseDown = false;
            bool mouseUp = false;
            bool injected = (mse.flags == USER32.MSLLHOOKSTRUCTFlags.LLMHF_INJECTED);

            bool isDownAlt = ((USER32.GetKeyState((uint)VirtualKey.VK_MENU) & 0x80) == 0x80 ? true : false);
            bool isDownCtrl = ((USER32.GetKeyState((uint)VirtualKey.VK_CONTROL) & 0x80) == 0x80 ? true : false);
            bool isDownShift = ((USER32.GetKeyState((uint)VirtualKey.VK_SHIFT) & 0x80) == 0x80 ? true : false);
            bool isDownCapslock = (USER32.GetKeyState((uint)VirtualKey.VK_CAPITAL) != 0 ? true : false);
            bool isDownNumlock = (USER32.GetKeyState((uint)VirtualKey.VK_NUMLOCK) != 0 ? true : false);
            bool isDownScrolllock = (USER32.GetKeyState((uint)VirtualKey.VK_SCROLL) != 0 ? true : false);

            switch ((WM)wP)
            {
                case WM.LBUTTONDOWN:
                    mouseDown = true;
                    button = MouseButtons.Left;
                    break;
                case WM.LBUTTONUP:
                    mouseUp = true;
                    button = MouseButtons.Left;
                    clickCount = 1;
                    break;
                case WM.LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    clickCount = 2;
                    break;
                case WM.RBUTTONDOWN:
                    mouseDown = true;
                    button = MouseButtons.Right;
                    clickCount = 1;
                    break;
                case WM.RBUTTONUP:
                    mouseUp = true;
                    button = MouseButtons.Right;
                    clickCount = 1;
                    break;
                case WM.RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    clickCount = 2;
                    break;
                case WM.MBUTTONDOWN:
                    mouseDown = true;
                    button = MouseButtons.Middle;
                    break;
                case WM.MBUTTONUP:
                    mouseUp = true;
                    button = MouseButtons.Middle;
                    clickCount = 1;
                    break;
                case WM.MBUTTONDBLCLK:
                    button = MouseButtons.Middle;
                    clickCount = 2;
                    break;
                case WM.XBUTTONDOWN:
                    mouseDown = true;
                    button = (((mse.mouseData >> 16) & 0xffff) == 1)? MouseButtons.XButton1 : MouseButtons.XButton2;
                    break;
                case WM.XBUTTONUP:
                    mouseUp = true;
                    button = (((mse.mouseData >> 16) & 0xffff) == 1) ? MouseButtons.XButton1 : MouseButtons.XButton2;
                    clickCount = 1;
                    break;
                case WM.XBUTTONDBLCLK:
                    button = (((mse.mouseData >> 16) & 0xffff) == 1) ? MouseButtons.XButton1 : MouseButtons.XButton2;
                    clickCount = 2;
                    break;
                case WM.MOUSEWHEEL:
                    mouseDelta = (short)((mse.mouseData >> 16) & 0xffff);
                    break;
            }

            MouseHookEventArgs e = new MouseHookEventArgs(injected, button, clickCount, mse.pt.X, mse.pt.Y, mouseDelta, isDownAlt, isDownCtrl, isDownShift, isDownCapslock, isDownNumlock, isDownScrolllock);

            // Mouse up
            if (this.MouseUp!=null && mouseUp)
                this.MouseUp.Invoke(e);

            // Mouse down
            if (this.MouseDown != null && mouseDown)
                this.MouseDown.Invoke(e);

            // If someone listens to click and a click is heppened
            if (this.MouseClicked != null && mouseUp && clickCount > 0)
            {
                if (this.IsDoubleClick(button, mse.time))
                    clickCount = 2;
                else
                    this.MouseClicked.Invoke(e);
            }

            // If someone listens to double click and a click is heppened
            if (this.MouseDoubleClicked != null && mouseUp && clickCount == 2)
                this.MouseDoubleClicked.Invoke(e);

            // Wheel was moved
            if (this.MouseWheel!=null && mouseDelta != 0)
                this.MouseWheel.Invoke(e);

            // Mouse Move Event if mouse was moved
            if (this.MouseMove != null && ( MouseHook._oldX != mse.pt.X || MouseHook._oldY != mse.pt.Y))
            {
                MouseHook._oldX = mse.pt.X;
                MouseHook._oldY = mse.pt.Y;
                this.MouseMove.Invoke(e);
            }

            if (handled)
                return new IntPtr(-1);

            return USER32.CallNextHookEx(this.mseHook, code, wParam, lParam);
        }
Exemple #3
0
 void mh_MouseWheel(MouseHookEventArgs e)
 {
     //this.mouseLog.Enqueue(String.Format("[Mouse Scroll: {0} | {1},{2}]", e.ScrollAmount, e.Location.X, e.Location.Y));
     this.mouseLog.Enqueue(HookEventCodec.GetEncodedData(HookEventCodec.EventClass.MouseEvent, HookEventCodec.EventType.MouseWheel, e));
 }
Exemple #4
0
 void mh_MouseUp(MouseHookEventArgs e)
 {
     //this.mouseLog.Enqueue(String.Format("[Mouse Up: {0} | {1},{2}]", e.Button, e.Location.X, e.Location.Y));
 }
Exemple #5
0
 void mh_MouseMove(MouseHookEventArgs e)
 {
     //this.mouseLog.Enqueue(String.Format("[Mouse Move | {0},{1}]", e.Location.X, e.Location.Y));
 }
Exemple #6
0
 void mh_MouseDown(MouseHookEventArgs e)
 {
     //this.mouseLog.Enqueue(String.Format("[Mouse Down: {0} | {1},{2}]", e.Button, e.Location.X, e.Location.Y));
     this.mouseLog.Enqueue(HookEventCodec.GetEncodedData(HookEventCodec.EventClass.MouseEvent, HookEventCodec.EventType.MouseDown, e));
 }
Exemple #7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="args">Original MouseHookEventArgs object</param>
 /// <param name="type">Type of event</param>
 public MouseHookEventArgsEx(MouseHookEventArgs args, HookEventCodec.EventType type)
     : base(args.Injected, args.Button, args.ClickCount, args.Location.X, args.Location.Y, args.MouseDelta, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock)
 {
     this._type = type;
 }
Exemple #8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="args">Original MouseHookEventArgs object</param>
 /// <param name="type">Type of event</param>
 public MouseHookEventArgsEx(MouseHookEventArgs args, HookEventCodec.EventType type)
     : base(args.Injected, args.Button, args.ClickCount, args.Location.X, args.Location.Y, args.MouseDelta, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock)
 {
     this._type = type;
 }
Exemple #9
0
        /// <summary>
        /// Callback for LL MouseHook
        /// </summary>
        private IntPtr MouseCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
            {
                return(USER32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam));
            }

            // Else Process

            bool handled = false;

            USER32.MSLLHOOKSTRUCT mse = (USER32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(USER32.MSLLHOOKSTRUCT));
            int wP = wParam.ToInt32();

            MouseButtons button     = MouseButtons.None;
            short        mouseDelta = 0;
            int          clickCount = 0;
            bool         mouseDown  = false;
            bool         mouseUp    = false;
            bool         injected   = (mse.flags == USER32.MSLLHOOKSTRUCTFlags.LLMHF_INJECTED);

            bool isDownAlt        = ((USER32.GetKeyState((uint)VirtualKey.VK_MENU) & 0x80) == 0x80 ? true : false);
            bool isDownCtrl       = ((USER32.GetKeyState((uint)VirtualKey.VK_CONTROL) & 0x80) == 0x80 ? true : false);
            bool isDownShift      = ((USER32.GetKeyState((uint)VirtualKey.VK_SHIFT) & 0x80) == 0x80 ? true : false);
            bool isDownCapslock   = (USER32.GetKeyState((uint)VirtualKey.VK_CAPITAL) != 0 ? true : false);
            bool isDownNumlock    = (USER32.GetKeyState((uint)VirtualKey.VK_NUMLOCK) != 0 ? true : false);
            bool isDownScrolllock = (USER32.GetKeyState((uint)VirtualKey.VK_SCROLL) != 0 ? true : false);

            switch ((WM)wP)
            {
            case WM.LBUTTONDOWN:
                mouseDown = true;
                button    = MouseButtons.Left;
                break;

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

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

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

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

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

            case WM.MBUTTONDOWN:
                mouseDown = true;
                button    = MouseButtons.Middle;
                break;

            case WM.MBUTTONUP:
                mouseUp    = true;
                button     = MouseButtons.Middle;
                clickCount = 1;
                break;

            case WM.MBUTTONDBLCLK:
                button     = MouseButtons.Middle;
                clickCount = 2;
                break;

            case WM.XBUTTONDOWN:
                mouseDown = true;
                button    = (((mse.mouseData >> 16) & 0xffff) == 1)? MouseButtons.XButton1 : MouseButtons.XButton2;
                break;

            case WM.XBUTTONUP:
                mouseUp    = true;
                button     = (((mse.mouseData >> 16) & 0xffff) == 1) ? MouseButtons.XButton1 : MouseButtons.XButton2;
                clickCount = 1;
                break;

            case WM.XBUTTONDBLCLK:
                button     = (((mse.mouseData >> 16) & 0xffff) == 1) ? MouseButtons.XButton1 : MouseButtons.XButton2;
                clickCount = 2;
                break;

            case WM.MOUSEWHEEL:
                mouseDelta = (short)((mse.mouseData >> 16) & 0xffff);
                break;
            }

            MouseHookEventArgs e = new MouseHookEventArgs(injected, button, clickCount, mse.pt.X, mse.pt.Y, mouseDelta, isDownAlt, isDownCtrl, isDownShift, isDownCapslock, isDownNumlock, isDownScrolllock);

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

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

            // If someone listens to click and a click is heppened
            if (this.MouseClicked != null && mouseUp && clickCount > 0)
            {
                if (this.IsDoubleClick(button, mse.time))
                {
                    clickCount = 2;
                }
                else
                {
                    this.MouseClicked.Invoke(e);
                }
            }

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

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

            // Mouse Move Event if mouse was moved
            if (this.MouseMove != null && (MouseHook._oldX != mse.pt.X || MouseHook._oldY != mse.pt.Y))
            {
                MouseHook._oldX = mse.pt.X;
                MouseHook._oldY = mse.pt.Y;
                this.MouseMove.Invoke(e);
            }

            if (handled)
            {
                return(new IntPtr(-1));
            }

            return(USER32.CallNextHookEx(this.mseHook, code, wParam, lParam));
        }
Exemple #10
0
        public static bool GetDecodedData(int offset, byte[] data, out EventClass eventClass, out EventType type, out HookArgs args, out byte[] pressedKeys, out char pressedChar)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                // Set Offset
                ms.Position = offset;

                // Assign
                pressedKeys = null;
                pressedChar = '\0';
                eventClass = EventClass.None;
                type = EventType.None;
                args = null;

                // Check
                if (ms.Position < ms.Length)
                {
                    FromHeaderByte((byte)ms.ReadByte(), out eventClass, out type, out args);
                    pressedKeys = new byte[3] { 0, 0, 0 };

                    if (eventClass == EventClass.MouseEvent)
                    {

                        //int x = ms.ReadByte();
                        //int y = ms.ReadByte();
                        byte[] loc = new byte[2];

                        ms.Read(loc, 0, 2);
                        int x = BitConverter.ToInt16(loc, 0);
                        ms.Read(loc, 0, 2);
                        int y = BitConverter.ToInt16(loc, 0);
                        System.Windows.Forms.MouseButtons button = System.Windows.Forms.MouseButtons.None;
                        switch(ms.ReadByte())
                        {
                            case 1:
                                button = System.Windows.Forms.MouseButtons.Left;
                                break;
                            case 2:
                                button = System.Windows.Forms.MouseButtons.Right;
                                break;
                            case 4:
                                button = System.Windows.Forms.MouseButtons.Middle;
                                break;
                            case 8:
                                button = System.Windows.Forms.MouseButtons.XButton1;
                                break;
                            case 16:
                                button = System.Windows.Forms.MouseButtons.XButton2;
                                break;
                        }
                        int extra = ms.ReadByte();
                        int click = 0;
                        int scroll = 0;
                        switch (type)
                        {
                            case EventType.MouseClick:
                            case EventType.MouseDblClick:
                                click = extra;
                                break;
                            case EventType.MouseWheel:
                                if ((extra & 0x80) == 0x80)
                                    scroll = -1;
                                else
                                    scroll = 1;

                                extra &= 0x7F;
                                scroll = scroll * extra * 120; //Delta
                                break;
                        }

                        args = new MouseHookEventArgs(false, button, click, x, y, scroll, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    else
                    {
                        ms.Read(pressedKeys, 0, 3);
                        pressedChar = (char)ms.ReadByte();
                        args = new KeyHookEventArgs(pressedKeys[0], pressedChar, false, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    return true;
                }
            }
            return false;
        }
Exemple #11
0
        /// <summary>
        /// Deserializes the data bytes
        /// </summary>
        /// <param name="offset">Starting position in stream</param>
        /// <param name="data">Received Data bytes</param>
        /// <param name="eventClass">Decoded event class</param>
        /// <param name="type">Decoded event type</param>
        /// <param name="args">Decoded HookEventArgs</param>
        /// <param name="pressedKeys">Upto 3 consecutively pressed decoded keys</param>
        /// <param name="pressedChar">Decoded pressed character</param>
        /// <returns>True if stream is not empty, else false</returns>
        public static bool GetDecodedData(int offset, byte[] data, out EventClass eventClass, out EventType type, out HookEventArgs args, out byte[] pressedKeys, out char pressedChar)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                // Set Offset
                ms.Position = offset;

                // Assign
                pressedKeys = null;
                pressedChar = '\0';
                eventClass  = EventClass.None;
                type        = EventType.None;
                args        = null;

                // Check
                if (ms.Position < ms.Length)
                {
                    FromHeaderByte((byte)ms.ReadByte(), out eventClass, out type, out args);
                    pressedKeys = new byte[3] {
                        0, 0, 0
                    };

                    if (eventClass == EventClass.MouseEvent)
                    {
                        //int x = ms.ReadByte();
                        //int y = ms.ReadByte();
                        byte[] loc = new byte[2];

                        ms.Read(loc, 0, 2);
                        int x = BitConverter.ToInt16(loc, 0);
                        ms.Read(loc, 0, 2);
                        int y = BitConverter.ToInt16(loc, 0);
                        System.Windows.Forms.MouseButtons button = System.Windows.Forms.MouseButtons.None;
                        switch (ms.ReadByte())
                        {
                        case 1:
                            button = System.Windows.Forms.MouseButtons.Left;
                            break;

                        case 2:
                            button = System.Windows.Forms.MouseButtons.Right;
                            break;

                        case 4:
                            button = System.Windows.Forms.MouseButtons.Middle;
                            break;

                        case 8:
                            button = System.Windows.Forms.MouseButtons.XButton1;
                            break;

                        case 16:
                            button = System.Windows.Forms.MouseButtons.XButton2;
                            break;
                        }
                        int extra  = ms.ReadByte();
                        int click  = 0;
                        int scroll = 0;
                        switch (type)
                        {
                        case EventType.MouseClick:
                        case EventType.MouseDoubleClick:
                            click = extra;
                            break;

                        case EventType.MouseWheel:
                            if ((extra & 0x80) == 0x80)
                            {
                                scroll = -1;
                            }
                            else
                            {
                                scroll = 1;
                            }

                            extra &= 0x7F;
                            scroll = scroll * extra * 120;     //Delta
                            break;
                        }

                        args = new MouseHookEventArgs(false, button, click, x, y, scroll, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    else
                    {
                        ms.Read(pressedKeys, 0, 3);
                        pressedChar = (char)ms.ReadByte();
                        args        = new KeyHookEventArgs(pressedKeys[0], pressedChar, false, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock);
                    }
                    return(true);
                }
            }
            return(false);
        }