Пример #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);
        }
Пример #2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="args">Original KeyHookEventArgs object</param>
 /// <param name="type">Type of event</param>
 /// <param name="pressedKeys">List of keys pressed simultaneously</param>
 /// <param name="pressedChar">Key chracter pressed</param>
 public KeyHookEventArgsEx(KeyHookEventArgs args, HookEventCodec.EventType type, List <System.Windows.Forms.Keys> pressedKeys, char pressedChar)
     : base((uint)args.KeyValue, pressedChar, args.Injected, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock)
 {
     this._type = type;
     if (pressedKeys == null)
     {
         this._pressedKeys = new List <System.Windows.Forms.Keys>();
     }
     else
     {
         this._pressedKeys = pressedKeys;
     }
 }
Пример #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="args">Original KeyHookEventArgs object</param>
 /// <param name="type">Type of event</param>
 /// <param name="pressedKeys">List of keys pressed simultaneously</param>
 /// <param name="pressedChar">Key chracter pressed</param>
 public KeyHookEventArgsEx(KeyHookEventArgs args, HookEventCodec.EventType type, List<System.Windows.Forms.Keys> pressedKeys, char pressedChar)
     : base((uint)args.KeyValue, pressedChar, args.Injected, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock)
 {
     this._type = type;
     if (pressedKeys == null)
         this._pressedKeys = new List<System.Windows.Forms.Keys>();
     else
         this._pressedKeys = pressedKeys;
 }
Пример #4
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;
        }
Пример #5
0
        /// <summary>
        /// Callback for LL Keyboard Hook
        /// </summary>
        private IntPtr KeyboardCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
            {
                return(USER32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam));
            }

            // Else Process

            bool handled = false;

            USER32.KBDLLHOOKSTRUCT kbd = (USER32.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(USER32.KBDLLHOOKSTRUCT));
            int wP = wParam.ToInt32();

            bool isInjected       = kbd.flags == USER32.KBDLLHOOKSTRUCTFlags.LLKHF_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);

            // If Key Down
            if (this.KeyDown != null && (wP == (int)WM.SYSKEYDOWN || wP == (int)WM.KEYDOWN))
            {
                KeyHookEventArgs e = new KeyHookEventArgs(kbd.vkCode, isInjected, isDownAlt, isDownCtrl, isDownShift, isDownCapslock, isDownNumlock, isDownScrolllock);
                this.KeyDown.Invoke(e);
                handled = e.Handled;
            }

            // Key Press
            if (this.KeyPressed != null && wP == (int)WM.KEYDOWN)
            {
                byte[] keyState = new byte[256];
                USER32.GetKeyboardState(keyState);
                byte[] inBuffer = new byte[2];
                if (USER32.ToAscii(kbd.vkCode, kbd.scanCode, keyState, inBuffer, (uint)kbd.flags) == 1)
                {
                    char key = (char)inBuffer[0];
                    if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                    {
                        key = Char.ToUpper(key);
                    }
                    KeyHookEventArgs e = new KeyHookEventArgs(kbd.vkCode, key, isInjected, isDownAlt, isDownCtrl, isDownShift, isDownCapslock, isDownNumlock, isDownScrolllock);
                    this.KeyPressed.Invoke(e);
                    handled = handled || e.Handled;
                }
            }

            // Key Up
            if (this.KeyUp != null && (wP == (int)WM.SYSKEYUP || wP == (int)WM.KEYUP))
            {
                Keys             keyData = (Keys)kbd.vkCode;
                KeyHookEventArgs e       = new KeyHookEventArgs(kbd.vkCode, isInjected, isDownAlt, isDownCtrl, isDownShift, isDownCapslock, isDownNumlock, isDownScrolllock);
                this.KeyUp.Invoke(e);
                handled = handled || e.Handled;
            }
            if (handled)
            {
                return(new IntPtr(-1));
            }
            return(USER32.CallNextHookEx(this.kbdHook, code, wParam, lParam));
        }
Пример #6
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);
        }