Exemple #1
0
        /// <summary>
        ///     Jiggle the mouse; i.e., fake a mouse movement event.
        /// </summary>
        /// <param name="delta">The mouse will be moved by delta pixels along both X and Y.</param>
        internal static void Jiggle(int delta)
        {
            var inp = new User32.INPUT
            {
                type   = User32.InputType.INPUT_MOUSE,
                Inputs = new User32.INPUT.InputUnion
                {
                    mi = new User32.MOUSEINPUT
                    {
                        dx                 = delta,
                        dy                 = delta,
                        mouseData          = 0,
                        dwFlags            = User32.MOUSEEVENTF.MOUSEEVENTF_MOVE,
                        time               = 0,
                        dwExtraInfo_IntPtr = IntPtr.Zero,
                    },
                },
            };

            uint returnValue = User32.SendInput(nInputs: 1, pInputs: new[] { inp, }, cbSize: Marshal.SizeOf <User32.INPUT> ());

            if (returnValue != 1)
            {
                int errorCode = Marshal.GetLastWin32Error();

                Debugger.Log(level: 1,
                             category: "Jiggle",
                             message:
                             $"failed to insert event to input stream; retval={returnValue}, errcode=0x{errorCode:x8}\n");
            }
        }
Exemple #2
0
        private void SendCharacter(char character)
        {
            var input = new User32.INPUT[] {
                new User32.INPUT()
                {
                    Type = User32.INPUT_KEYBOARD,
                    ki   = new User32.KEYBDINPUT()
                    {
                        wVk         = 0,
                        wScan       = character,
                        dwFlags     = User32.KEYEVENTF_UNICODE,
                        time        = 0,
                        dwExtraInfo = IntPtr.Zero
                    }
                },
                new User32.INPUT()
                {
                    Type = User32.INPUT_KEYBOARD,
                    ki   = new User32.KEYBDINPUT()
                    {
                        wVk         = 0,
                        wScan       = character,
                        dwFlags     = User32.KEYEVENTF_UNICODE | User32.KEYEVENTF_KEYUP,
                        time        = 0,
                        dwExtraInfo = IntPtr.Zero
                    }
                }
            };

            IntegrationHelper.SendInput(input);
        }
Exemple #3
0
        public uint SendMouseWheel(int deltaY, Viewer viewer)
        {
            Win32Interop.SwitchToInputDesktop();
            if (deltaY < 0)
            {
                deltaY = -120;
            }
            else if (deltaY > 0)
            {
                deltaY = 120;
            }
            var union = new User32.InputUnion()
            {
                mi = new User32.MOUSEINPUT()
                {
                    dwFlags = MOUSEEVENTF.WHEEL, dx = 0, dy = 0, time = 0, mouseData = deltaY, dwExtraInfo = GetMessageExtraInfo()
                }
            };
            var input = new User32.INPUT()
            {
                type = InputType.MOUSE, U = union
            };

            return(SendInput(1, new User32.INPUT[] { input }, INPUT.Size));
        }
Exemple #4
0
 public void SendMouseWheel(int deltaY, Viewer viewer)
 {
     TryOnInputDesktop(() =>
     {
         if (deltaY < 0)
         {
             deltaY = -120;
         }
         else if (deltaY > 0)
         {
             deltaY = 120;
         }
         var union = new User32.InputUnion()
         {
             mi = new User32.MOUSEINPUT()
             {
                 dwFlags = MOUSEEVENTF.WHEEL, dx = 0, dy = 0, time = 0, mouseData = deltaY, dwExtraInfo = GetMessageExtraInfo()
             }
         };
         var input = new User32.INPUT()
         {
             type = InputType.MOUSE, U = union
         };
         SendInput(1, new User32.INPUT[] { input }, INPUT.Size);
     });
 }
Exemple #5
0
 void SendMouseInput(Point point, User32.MOUSEEVENTF flags, long timestamp)
 {
     User32.INPUT[] input = new User32.INPUT[1];
     input[0].type         = User32.SendInputType.Mouse;
     input[0].U.mi.dx      = (int)(point.X * 65535.0);
     input[0].U.mi.dy      = (int)(point.Y * 65535.0);
     input[0].U.mi.dwFlags = flags;
     input[0].U.mi.time    = 0; //(uint)timestamp / 1000;
     User32.SendInput(1, input, Marshal.SizeOf(typeof(User32.INPUT)));
 }
Exemple #6
0
        public static void SendMouseMove(int x, int y)
        {
            var inputs = new User32.INPUT[1];

            inputs[0].type              = User32.InputType.INPUT_MOUSE;
            inputs[0].Inputs.mi.dx      = x;
            inputs[0].Inputs.mi.dy      = y;
            inputs[0].Inputs.mi.dwFlags = User32.MOUSEEVENTF.MOUSEEVENTF_MOVE | User32.MOUSEEVENTF.MOUSEEVENTF_ABSOLUTE;

            SendInput(1, inputs);
        }
Exemple #7
0
        static void SendCharacter(char ch)
        {
            var inputs = new User32.INPUT[2];

            inputs[0].type          = User32.InputType.INPUT_KEYBOARD;
            inputs[0].Inputs.ki.wVk = (User32.VirtualKey)ch;

            inputs[1].type              = User32.InputType.INPUT_KEYBOARD;
            inputs[1].Inputs.ki.wVk     = (User32.VirtualKey)ch;
            inputs[1].Inputs.ki.dwFlags = User32.KEYEVENTF.KEYEVENTF_KEYUP;

            SendInput(inputs.Length, inputs);
        }
Exemple #8
0
        public static void SendUnicodeChar(char input)
        {
            var inputStruct = new User32.INPUT();

            inputStruct.type            = User32.INPUTTYPE.Keyboard;
            inputStruct.u.ki.virtualKey = 0;
            inputStruct.u.ki.scanCode   = input;
            inputStruct.u.ki.time       = 0;
            inputStruct.u.ki.flags      = User32.KEYEVENTF.UNICODE;
            inputStruct.u.ki.extraInfo  = User32.GetMessageExtraInfo();

            User32.INPUT[] ip = { inputStruct };
            User32.SendInput(1, ip, Marshal.SizeOf(inputStruct));
        }
        private unsafe static void SendMouseInput(int x, int y, User32.MOUSEEVENTF flags)
        {
            if ((flags & User32.MOUSEEVENTF.ABSOLUTE) != 0)
            {
                int vscreenWidth  = User32.GetSystemMetrics(User32.SystemMetric.SM_CXVIRTUALSCREEN);
                int vscreenHeight = User32.GetSystemMetrics(User32.SystemMetric.SM_CYVIRTUALSCREEN);
                int vscreenLeft   = User32.GetSystemMetrics(User32.SystemMetric.SM_XVIRTUALSCREEN);
                int vscreenTop    = User32.GetSystemMetrics(User32.SystemMetric.SM_YVIRTUALSCREEN);

                const int DesktopNormilizedMax = 65536;

                // Absolute input requires that input is in 'normalized' coords - with the entire
                // desktop being (0,0)...(65535,65536). Need to convert input x,y coords to this
                // first.
                //
                // In this normalized world, any pixel on the screen corresponds to a block of values
                // of normalized coords - eg. on a 1024x768 screen,
                // y pixel 0 corresponds to range 0 to 85.333,
                // y pixel 1 corresponds to range 85.333 to 170.666,
                // y pixel 2 correpsonds to range 170.666 to 256 - and so on.
                // Doing basic scaling math - (x-top)*65536/Width - gets us the start of the range.
                // However, because int math is used, this can end up being rounded into the wrong
                // pixel. For example, if we wanted pixel 1, we'd get 85.333, but that comes out as
                // 85 as an int, which falls into pixel 0's range - and that's where the pointer goes.
                // To avoid this, we add on half-a-"screen pixel"'s worth of normalized coords - to
                // push us into the middle of any given pixel's range - that's the 65536/(Width*2)
                // part of the formula. So now pixel 1 maps to 85+42 = 127 - which is comfortably
                // in the middle of that pixel's block.
                // The key ting here is that unlike points in coordinate geometry, pixels take up
                // space, so are often better treated like rectangles - and if you want to target
                // a particular pixel, target its rectangle's midpoint, not its edge.
                x = ((x - vscreenLeft) * DesktopNormilizedMax) / vscreenWidth + DesktopNormilizedMax / (vscreenWidth * 2);
                y = ((y - vscreenTop) * DesktopNormilizedMax) / vscreenHeight + DesktopNormilizedMax / (vscreenHeight * 2);

                flags |= User32.MOUSEEVENTF.VIRTUALDESK;
            }

            var mouseInput = new User32.INPUT();

            mouseInput.type                      = User32.INPUTENUM.MOUSE;
            mouseInput.inputUnion.mi.dx          = x;
            mouseInput.inputUnion.mi.dy          = y;
            mouseInput.inputUnion.mi.mouseData   = 0;
            mouseInput.inputUnion.mi.dwFlags     = flags;
            mouseInput.inputUnion.mi.time        = 0;
            mouseInput.inputUnion.mi.dwExtraInfo = IntPtr.Zero;

            User32.SendInput(1, &mouseInput, Marshal.SizeOf(mouseInput));
        }
Exemple #10
0
        public static void SendMouseClick(int x, int y)
        {
            var inputs = new User32.INPUT[3];

            inputs[0].type              = User32.InputType.INPUT_MOUSE;
            inputs[0].Inputs.mi.dx      = x;
            inputs[0].Inputs.mi.dy      = y;
            inputs[0].Inputs.mi.dwFlags = User32.MOUSEEVENTF.MOUSEEVENTF_MOVE | User32.MOUSEEVENTF.MOUSEEVENTF_ABSOLUTE;

            inputs[1].type = User32.InputType.INPUT_MOUSE;
            inputs[1].Inputs.mi.dwFlags = User32.MOUSEEVENTF.MOUSEEVENTF_LEFTDOWN;

            inputs[2].type = User32.InputType.INPUT_MOUSE;
            inputs[2].Inputs.mi.dwFlags = User32.MOUSEEVENTF.MOUSEEVENTF_LEFTUP;

            SendInput(3, inputs);
        }
Exemple #11
0
        public static void SendKeyPressedEvent(Keys code, bool isKeyUp)
        {
            var input = new User32.INPUT();

            //TODO: consider moving this code into a factory
            input.type       = User32.INPUTTYPE.INPUT_KEYBOARD;
            input.ki         = new User32.KEYBDINPUT();
            input.ki.time    = 0;
            input.ki.dwFlags = isKeyUp
                ? User32.KEYEVENTF.KEYEVENTF_KEYUP | User32.KEYEVENTF.KEYEVENTF_SCANCODE
                : 0 | User32.KEYEVENTF.KEYEVENTF_SCANCODE;
            input.ki.dwExtraInfo = UIntPtr.Zero;
            input.ki.wScan       = (ushort)KeysHelper.GetScanCode(code);
            input.ki.wVk         = 0;

            User32.SendInput(1, new[] { input }, Marshal.SizeOf(typeof(User32.INPUT)));
        }
        private unsafe void SendKey(Keys key, bool down)
        {
            var input = new User32.INPUT();

            input.type = User32.INPUTENUM.KEYBOARD;
            input.inputUnion.ki.wVk         = (ushort)key;
            input.inputUnion.ki.dwFlags     = 0;
            input.inputUnion.ki.dwExtraInfo = IntPtr.Zero;
            input.inputUnion.ki.time        = 0;
            input.inputUnion.ki.wScan       = 0;

            if (!down)
            {
                input.inputUnion.ki.dwFlags |= User32.KEYEVENTF.KEYUP;
            }

            User32.SendInput(1, &input, Marshal.SizeOf(input));
        }
        private static uint SendKeyAction(bool keyDown, User32.VirtualKey virtualKey)
        {
            var key = new User32.INPUT
            {
                type   = User32.InputType.INPUT_KEYBOARD,
                Inputs = new User32.INPUT.InputUnion()
                {
                    ki = new User32.KEYBDINPUT()
                    {
                        dwFlags = (keyDown ? 0 : User32.KEYEVENTF.KEYEVENTF_KEYUP),
                        wVk     = virtualKey
                    }
                }
            };
            var inputs   = new[] { key };
            var response = User32.SendInput(inputs.Length, inputs, Marshal.SizeOf(key));

            return(response);
        }
Exemple #14
0
        public static void SendKey(Keys key)
        {
            int vk   = (int)key;
            int scan = User32.MapVirtualKey(vk, User32.MapVirtualKeyTranslation.MAPVK_VK_TO_VSC);

            User32.INPUT[] inputs = new User32.INPUT[2];
            inputs[0].type                         = User32.InputType.INPUT_KEYBOARD;
            inputs[0].Inputs.ki.wVk                = (User32.VirtualKey)vk;
            inputs[0].Inputs.ki.wScan              = (User32.ScanCode)scan;
            inputs[0].Inputs.ki.dwFlags            = 0;
            inputs[0].Inputs.ki.dwExtraInfo_IntPtr = IntPtr.Zero;
            inputs[0].Inputs.ki.time               = 0;
            inputs[1].type                         = User32.InputType.INPUT_KEYBOARD;
            inputs[1].Inputs.ki.wVk                = (User32.VirtualKey)vk;
            inputs[1].Inputs.ki.wScan              = (User32.ScanCode)scan;
            inputs[1].Inputs.ki.dwFlags            = User32.KEYEVENTF.KEYEVENTF_KEYUP;
            inputs[1].Inputs.ki.dwExtraInfo_IntPtr = IntPtr.Zero;
            inputs[1].Inputs.ki.time               = 0;
            User32.SendInput(inputs.Length, inputs, Marshal.SizeOf(inputs[0]));
        }
        private static uint SendKeyAction(bool keyDown, User32.ScanCode scanCode, bool extended)
        {
            var key = new User32.INPUT
            {
                type   = User32.InputType.INPUT_KEYBOARD,
                Inputs = new User32.INPUT.InputUnion()
                {
                    ki = new User32.KEYBDINPUT()
                    {
                        dwFlags = User32.KEYEVENTF.KEYEVENTF_SCANCODE |
                                  (extended ? User32.KEYEVENTF.KEYEVENTF_EXTENDED_KEY : 0) |
                                  (keyDown ? 0 : User32.KEYEVENTF.KEYEVENTF_KEYUP),
                        wScan = scanCode
                    }
                }
            };
            var inputs   = new[] { key };
            var response = User32.SendInput(inputs.Length, inputs, Marshal.SizeOf(key));

            return(response);
        }
Exemple #16
0
        static private void SetKey(KeyboardKey key, bool down)
        {
            if (is_down[(ushort)key] == down)
            {
                return;
            }

            is_down[(ushort)key] = down;

            User32.INPUT[] input_data = new User32.INPUT[1];
            input_data[0] = new User32.INPUT();

            input_data[0].type = (int)User32.InputType.INPUT_KEYBOARD;


            input_data[0].ki.dwFlags = 0;
            if ((int)key > 0x8000)
            {
                input_data[0].ki.wVk = (ushort)(key - 0x8000);
                // input_data[0].ki.dwFlags |= (uint)User32.SendInputFlags.KEYEVENTF_EXTENDEDKEY;
            }
            else
            {
                input_data[0].ki.wScan    = (ushort)key;
                input_data[0].ki.dwFlags |= (uint)User32.SendInputFlags.KEYEVENTF_SCANCODE;
            }

            if (!down)
            {
                input_data[0].ki.dwFlags |= (uint)User32.SendInputFlags.KEYEVENTF_KEYUP;
            }

            uint result = User32.SendInput(1, input_data, Marshal.SizeOf(input_data[0]));

            if (result != 1)
            {
                Console.WriteLine("SendInput result != 1 {=" + result + "}");
            }
        }
Exemple #17
0
        private void SendKey(ushort vk, uint dwFlags)
        {
            var input = new User32.INPUT[] {
                new User32.INPUT()
                {
                    Type = User32.INPUT_KEYBOARD,
                    ki   = new User32.KEYBDINPUT()
                    {
                        wVk         = vk,
                        wScan       = 0,
                        dwFlags     = dwFlags,
                        time        = 0,
                        dwExtraInfo = IntPtr.Zero
                    }
                }
            };

            if (IsExtendedKey(vk))
            {
                input[0].ki.dwFlags |= User32.KEYEVENTF_EXTENDEDKEY;
            }

            IntegrationHelper.SendInput(input);
        }
Exemple #18
0
        private void SendZalgo(object amount)
        {
            Thread.Sleep(1); // wait for event to be registered

            User32.INPUT[] inputs = new User32.INPUT[(int) amount * 2];

            char previousZalgo = '\u0000';
            for (int i = 0; i < inputs.Length; i++)
            {
                if (i % 2 == 0) previousZalgo = GetZalgo();

                inputs[i].type = 1; // keyboard event
                inputs[i].U.ki.wVk = 0;
                inputs[i].U.ki.wScan = (User32.ScanCodeShort) previousZalgo;
                inputs[i].U.ki.dwFlags =
                    i % 2 == 0 ?
                    User32.KEYEVENTF.UNICODE :
                    User32.KEYEVENTF.UNICODE | User32.KEYEVENTF.KEYUP;

                inputs[i].U.ki.time = 0;
                inputs[i].U.ki.dwExtraInfo = UIntPtr.Zero;
            }

            User32.SendInput((uint) inputs.Length, inputs, Marshal.SizeOf<User32.INPUT>());
        }
Exemple #19
0
        public static void SendKey(bool sendDown, bool sendUp, KeyName keyName)
        {
            var code = User32.VirtualKey.VK_NO_KEY;

            switch (keyName)
            {
            case KeyName.Backspace: code = User32.VirtualKey.VK_BACK; break;

            case KeyName.Tab: code = User32.VirtualKey.VK_TAB; break;

            case KeyName.Enter: code = User32.VirtualKey.VK_RETURN; break;

            case KeyName.Space: code = User32.VirtualKey.VK_SPACE; break;

            case KeyName.Home: code = User32.VirtualKey.VK_HOME; break;

            case KeyName.End: code = User32.VirtualKey.VK_END; break;

            case KeyName.Delete: code = User32.VirtualKey.VK_DELETE; break;

            case KeyName.PageUp: code = User32.VirtualKey.VK_PRIOR; break;

            case KeyName.PageDown: code = User32.VirtualKey.VK_NEXT; break;

            case KeyName.ArrowUp: code = User32.VirtualKey.VK_UP; break;

            case KeyName.ArrowDown: code = User32.VirtualKey.VK_DOWN; break;

            case KeyName.ArrowLeft: code = User32.VirtualKey.VK_LEFT; break;

            case KeyName.ArrowRight: code = User32.VirtualKey.VK_RIGHT; break;

            case KeyName.F1: code = User32.VirtualKey.VK_F1; break;

            case KeyName.F2: code = User32.VirtualKey.VK_F2; break;

            case KeyName.F3: code = User32.VirtualKey.VK_F3; break;

            case KeyName.F4: code = User32.VirtualKey.VK_F4; break;

            case KeyName.F5: code = User32.VirtualKey.VK_F5; break;

            case KeyName.F6: code = User32.VirtualKey.VK_F6; break;

            case KeyName.F7: code = User32.VirtualKey.VK_F7; break;

            case KeyName.F8: code = User32.VirtualKey.VK_F8; break;

            case KeyName.F9: code = User32.VirtualKey.VK_F9; break;

            case KeyName.F10: code = User32.VirtualKey.VK_F10; break;

            case KeyName.F11: code = User32.VirtualKey.VK_F11; break;

            case KeyName.F12: code = User32.VirtualKey.VK_F12; break;

            case KeyName.Escape: code = User32.VirtualKey.VK_ESCAPE; break;
            }

            if (code != 0 && (sendDown || sendUp))
            {
                var inputs = new User32.INPUT[2];

                inputs[0].type              = User32.InputType.INPUT_KEYBOARD;
                inputs[0].Inputs.ki.wVk     = code;
                inputs[0].Inputs.ki.dwFlags = sendDown ? 0 : User32.KEYEVENTF.KEYEVENTF_KEYUP;

                inputs[1].type              = User32.InputType.INPUT_KEYBOARD;
                inputs[1].Inputs.ki.wVk     = code;
                inputs[1].Inputs.ki.dwFlags = User32.KEYEVENTF.KEYEVENTF_KEYUP;

                SendInput((sendUp && sendDown) ? 2 : 1, inputs);
            }
        }
Exemple #20
0
        public static void SendText(bool isShift, bool isCtrl, bool isAlt, bool isWindows, String text)
        {
            var length = text.Length;

            for (var i = 0; i < length; i++)
            {
                var ch = text[i];

                var code = VkKeyScan(ch);

                if ((code & 0xFFFF) != 0xFFFF)
                {
                    var isShiftNeeded = isShift || (code & 0x0100) != 0;
                    var isCtrlNeeded  = isCtrl || (code & 0x0200) != 0;
                    var isAltNeeded   = isAlt || (code & 0x0400) != 0;
                    //var isHankakuNeeded = !!(code & 0x0800);

                    var inputs = new User32.INPUT[8];

                    var count = 0;

                    if (isShiftNeeded)
                    {
                        inputs[count].type          = User32.InputType.INPUT_KEYBOARD;
                        inputs[count].Inputs.ki.wVk = User32.VirtualKey.VK_SHIFT;
                        count++;
                    }

                    if (isCtrlNeeded)
                    {
                        inputs[count].type          = User32.InputType.INPUT_KEYBOARD;
                        inputs[count].Inputs.ki.wVk = User32.VirtualKey.VK_CONTROL;
                        count++;
                    }

                    if (isAltNeeded)
                    {
                        inputs[count].type          = User32.InputType.INPUT_KEYBOARD;
                        inputs[count].Inputs.ki.wVk = User32.VirtualKey.VK_MENU;
                        count++;
                    }

                    inputs[count].type          = User32.InputType.INPUT_KEYBOARD;
                    inputs[count].Inputs.ki.wVk = (User32.VirtualKey)((int)code & 0xFF);
                    count++;

                    var countdown = count;
                    while (countdown != 0)
                    {
                        countdown--;

                        inputs[count].type              = inputs[countdown].type;
                        inputs[count].Inputs.ki.wVk     = inputs[countdown].Inputs.ki.wVk;
                        inputs[count].Inputs.ki.dwFlags = inputs[countdown].Inputs.ki.dwFlags ^ User32.KEYEVENTF.KEYEVENTF_KEYUP;
                        count++;
                    }

                    SendInput(count, inputs);
                }
                else
                {
                    Debug.WriteLine($"Cannot send {ch}");
                }
            }
        }