Ejemplo n.º 1
0
        public static void ApplyPacket(MouseStatePacketWrapperType packetwrapper, BitField oldbuttons)
        {
            var input = new Input {
                type = SendInputEventType.InputMouse
            };

            input.mkhi.mi.dwFlags   = MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_MOVE_NOCOALESCE | MouseEventFlags.MOUSEEVENTF_WHEEL;
            input.mkhi.mi.dx        = packetwrapper.XDelta;
            input.mkhi.mi.dy        = packetwrapper.YDelta;
            input.mkhi.mi.mouseData = packetwrapper.Packet.WheelDelta;

            if (packetwrapper.Buttons[0] != oldbuttons[0])
            {
                input.mkhi.mi.dwFlags |= packetwrapper.Buttons[0] ? MouseEventFlags.MOUSEEVENTF_LEFTDOWN : MouseEventFlags.MOUSEEVENTF_LEFTUP;
            }
            if (packetwrapper.Buttons[1] != oldbuttons[1])
            {
                input.mkhi.mi.dwFlags |= packetwrapper.Buttons[1] ? MouseEventFlags.MOUSEEVENTF_RIGHTDOWN : MouseEventFlags.MOUSEEVENTF_RIGHTUP;
            }
            if (packetwrapper.Buttons[2] != oldbuttons[2])
            {
                input.mkhi.mi.dwFlags |= packetwrapper.Buttons[2] ? MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN : MouseEventFlags.MOUSEEVENTF_MIDDLEUP;
            }

            SendInput(1, new[] { input }, Marshal.SizeOf <Input>());
        }
Ejemplo n.º 2
0
        public MainForm()
        {
            InitializeComponent();

            ListenerThread = new Thread(() =>
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(new IPEndPoint(IPAddress.Any, Settings.Default.ListeningPort));

                var oldbuttons  = new BitField(8);
                var oldkeystate = new BitField(256);

                var recvbuffer        = new byte[40];
                var rawmousepacket    = new byte[Marshal.SizeOf <MouseStatePacketType>()];
                var rawkeyboardpacket = new byte[Marshal.SizeOf <KeyboardStatePacketType>()];

                while (true)
                {
                    socket.Receive(recvbuffer);

                    switch (recvbuffer[0])
                    {
                    case 1:
                        {
                            // mouse
                            Array.Copy(recvbuffer, rawmousepacket, rawmousepacket.Length);
                            var packetwrapper = new MouseStatePacketWrapperType(rawmousepacket.ToObject <MouseStatePacketType>());
                            InputSimulator.ApplyPacket(packetwrapper, oldbuttons);
                            oldbuttons.CopyFrom(packetwrapper.Buttons);
                        }

                        break;

                    case 2:
                        {
                            // keyboard
                            Array.Copy(recvbuffer, rawkeyboardpacket, rawkeyboardpacket.Length);
                            var packetwrapper = new KeyboardStatePacketWrapperType(rawkeyboardpacket.ToObject <KeyboardStatePacketType>());
                            InputSimulator.ApplyPacket(packetwrapper, oldkeystate);
                            oldkeystate.CopyFrom(packetwrapper.KeyState);
                        }

                        break;

                    default:
                        continue;
                    }
                }
            })
            {
                Name = "Listener Thread", IsBackground = true
            };
            ListenerThread.Start();
        }