Beispiel #1
0
        /// <summary>
        /// Simulates a mouse right button double-click gesture.
        /// </summary>
        public IMouseSimulator RightButtonDoubleClick()
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddMouseButtonDoubleClick(MouseButton.RightButton).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #2
0
        /// <summary>
        /// Simulates a mouse horizontal wheel scroll gesture. Supported by Windows Vista and later.
        /// </summary>
        /// <param name="scrollAmountInClicks">The amount to scroll in clicks. A positive value indicates that the wheel was rotated to the right; a negative value indicates that the wheel was rotated to the left.</param>
        public IMouseSimulator HorizontalScroll(int scrollAmountInClicks)
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddMouseHorizontalWheelScroll(scrollAmountInClicks * MouseWheelClickSize).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #3
0
        /// <summary>
        /// Simulates a mouse left button up gesture.
        /// </summary>
        public IMouseSimulator LeftButtonUp()
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddMouseButtonUp(MouseButton.LeftButton).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #4
0
        /// <summary>
        /// Simulates mouse movement by the specified distance measured as a delta from the current mouse location in pixels.
        /// </summary>
        /// <param name="pixelDeltaX">The distance in pixels to move the mouse horizontally.</param>
        /// <param name="pixelDeltaY">The distance in pixels to move the mouse vertically.</param>
        public IMouseSimulator MoveMouseBy(int pixelDeltaX, int pixelDeltaY)
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddRelativeMouseMovement(pixelDeltaX, pixelDeltaY).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #5
0
        /// <summary>
        /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
        /// </summary>
        /// <param name="keyCode">The <see cref="VirtualKeyCode"/> to press</param>
        public void KeyPress(VirtualKeyCode keyCode)
        {
            var inputList =
                new InputBuilder()
                .AddKeyDown(keyCode)
                .AddKeyUp(keyCode)
                .ToArray();

            SendSimulatedInput(inputList);
        }
Beispiel #6
0
 /// <summary>
 /// Calls the Win32 SendInput method to simulate a KeyUp.
 /// </summary>
 /// <param name="keyCode">The <see cref="VirtualKeyCode"/> to lift up</param>
 public IKeyboardSimulator KeyUp(VirtualKeyCode keyCode)
 {
     unsafe
     {
         var input = stackalloc INPUT[1];
         input[0] = InputBuilder.MakeKeyUp(keyCode, _inputSimulator.ExtraInfo);
         _messageDispatcher.DispatchInput(input, 1);
     }
     return(this);
 }
Beispiel #7
0
        /// <summary>
        /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard.
        /// </summary>
        /// <param name="text">The text to be simulated.</param>
        public void TextEntry(string text)
        {
            if (text.Length > UInt32.MaxValue / 2)
            {
                throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");
            }
            var inputList = new InputBuilder().AddCharacters(text).ToArray();

            SendSimulatedInput(inputList);
        }
Beispiel #8
0
        /// <summary>
        /// Simulates a single character text entry via the keyboard.
        /// </summary>
        /// <param name="character">The unicode character to be simulated.</param>
        public IKeyboardSimulator TextEntry(char character)
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddCharacter(character).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #9
0
        /// <summary>
        /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys.
        /// The flow is Modifiers KeyDown in order, Keys Press in order, Modifiers KeyUp in reverse order.
        /// </summary>
        /// <param name="modifierDikCodes">The list of modifier keys</param>
        /// <param name="dikCodes">The list of keys to simulate</param>
        public IKeyboardSimulator ModifiedKeyStroke(IEnumerable <DirectInputKeyCode> modifierDikCodes, IEnumerable <DirectInputKeyCode> dikCodes)
        {
            var builder = new InputBuilder();

            ModifiersDown(builder, modifierDikCodes);
            KeysPress(builder, dikCodes);
            ModifiersUp(builder, modifierDikCodes);
            SendSimulatedInput(builder.ToArray());
            return(this);
        }
Beispiel #10
0
        /// <summary>
        /// Simulates a mouse X button double-click gesture.
        /// </summary>
        /// <param name="buttonId">The button id.</param>
        public IMouseSimulator XButtonDoubleClick(int buttonId)
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddMouseXButtonDoubleClick(buttonId).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #11
0
        /// <summary>
        /// Simulates mouse movement to the specified location on the primary display device.
        /// </summary>
        /// <param name="absoluteX">The destination's absolute X-coordinate on the primary display device where 0 is the extreme left hand side of the display device and 65535 is the extreme right hand side of the display device.</param>
        /// <param name="absoluteY">The destination's absolute Y-coordinate on the primary display device where 0 is the top of the display device and 65535 is the bottom of the display device.</param>
        public IMouseSimulator MoveMouseTo(double absoluteX, double absoluteY)
        {
            var inputList = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            }.AddAbsoluteMouseMovement((int)Math.Truncate(absoluteX), (int)Math.Truncate(absoluteY)).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #12
0
        /// <summary>
        /// Simulates a simple modified keystroke like CTRL-C where CTRL is the modifierKey and C is the key.
        /// The flow is Modifier KeyDown, Key Press, Modifier KeyUp.
        /// </summary>
        /// <param name="modifierKeyCode">The modifier key</param>
        /// <param name="keyCode">The key to simulate</param>
        public void ModifiedKeyStroke(VirtualKeyCode modifierKeyCode, VirtualKeyCode keyCode)
        {
            var inputList =
                new InputBuilder()
                .AddKeyDown(modifierKeyCode)
                .AddKeyPress(keyCode)
                .AddKeyUp(modifierKeyCode)
                .ToArray();

            SendSimulatedInput(inputList);
        }
 private void KeysPress(InputBuilder builder, IEnumerable <VirtualKeyCode> keyCodes)
 {
     if (keyCodes == null)
     {
         return;
     }
     foreach (var key in keyCodes)
     {
         builder.AddKeyPress(key);
     }
 }
Beispiel #14
0
 private void ModifiersDown(InputBuilder builder, IEnumerable <DirectInputKeyCode> modifierDikCodes)
 {
     if (modifierDikCodes == null)
     {
         return;
     }
     foreach (var dik in modifierDikCodes)
     {
         builder.AddKeyDown(dik);
     }
 }
Beispiel #15
0
        /// <summary>
        /// Simulates a key press for each of the specified key codes in the order they are specified.
        /// </summary>
        /// <param name="keyCodes"></param>
        public IKeyboardSimulator KeyPress(params VirtualKeyCode[] keyCodes)
        {
            var builder = new InputBuilder()
            {
                ExtraInfo = _inputSimulator.ExtraInfo
            };

            KeysPress(builder, keyCodes);
            SendSimulatedInput(builder.ToArray());
            return(this);
        }
Beispiel #16
0
 private void KeysPress(InputBuilder builder, IEnumerable <DirectInputKeyCode> dikCodes)
 {
     if (dikCodes == null)
     {
         return;
     }
     foreach (var dik in dikCodes)
     {
         builder.AddKeyPress(dik);
     }
 }
 private void ModifiersDown(InputBuilder builder, IEnumerable <VirtualKeyCode> modifierKeyCodes)
 {
     if (modifierKeyCodes == null)
     {
         return;
     }
     foreach (var key in modifierKeyCodes)
     {
         builder.AddKeyDown(key);
     }
 }
Beispiel #18
0
        /// <summary>
        /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
        /// </summary>
        /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to press</param>
        public IKeyboardSimulator KeyPress(DirectInputKeyCode dikCode)
        {
            var inputList =
                new InputBuilder()
                .AddKeyDown(dikCode)
                .AddKeyUp(dikCode)
                .ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
        /// <summary>
        /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys.
        /// The flow is Modifiers KeyDown in order, Keys Press in order, Modifiers KeyUp in reverse order.
        /// </summary>
        /// <param name="modifierKeyCodes">The list of modifier keys</param>
        /// <param name="keyCodes">The list of keys to simulate</param>
        public IKeyboardSimulator ModifiedKeyStroke(IEnumerable <VirtualKeyCode> modifierKeyCodes, IEnumerable <VirtualKeyCode> keyCodes)
        {
            var builder = new InputBuilder(UseScanCodes);

            ModifiersDown(builder, modifierKeyCodes);
            KeysPress(builder, keyCodes);
            ModifiersUp(builder, modifierKeyCodes);

            SendSimulatedInput(builder.ToArray());
            return(this);
        }
        /// <summary>
        /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard.
        /// </summary>
        /// <param name="text">The text to be simulated.</param>
        public IKeyboardSimulator TextEntry(string text)
        {
            if (text.Length > uint.MaxValue / 2)
            {
                throw new ArgumentException(
                          $"The text parameter is too long. It must be less than {uint.MaxValue/2} characters.", nameof(text));
            }
            var inputList = new InputBuilder().AddCharacters(text).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #21
0
        /// <summary>
        /// Calls the Win32 SendInput method to simulate a KeyDown.
        /// </summary>
        /// <param name="keyCode">The <see cref="VirtualKeyCode"/> to press</param>
        public IKeyboardSimulator KeyDown(VirtualKeyCode keyCode)
        {
            //var inputList = new InputBuilder(){ ExtraInfo = _inputSimulator.ExtraInfo }.AddKeyDown(keyCode).ToArray();
            unsafe
            {
                var input = stackalloc INPUT[1];
                input[0] = InputBuilder.MakeKeyDown(keyCode, _inputSimulator.ExtraInfo);
                _messageDispatcher.DispatchInput(input, 1);
            }

            return(this);
        }
        /// <summary>
        /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys.
        /// The flow is Keys Press in order, Modifiers KeyUp in reverse order.
        /// </summary>
        /// <param name="modifierDikCodes">The list of modifier keys</param>
        /// <param name="dikCode">The list of keys to simulate</param>
        /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param>
        public IKeyboardSimulator DelayedModifiedKeyStrokeUp(IEnumerable <DirectInputKeyCode> modifierDikCodes, DirectInputKeyCode dikCode, int delay)
        {
            KeyUp(dikCode);

            foreach (var keyCode in modifierDikCodes.Reverse())
            {
                var inputList = new InputBuilder().AddKeyUp(keyCode).ToArray();
                SendSimulatedInput(inputList);
            }

            return(this);
        }
        /// <summary>
        /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard.
        /// </summary>
        /// <param name="text">The text to be simulated.</param>
        public IKeyboardSimulator TextEntry(string text)
        {
            ////Debug.WriteLine("TextEntry: " + text);
            if (text.Length > UInt32.MaxValue / 2)
            {
                throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");
            }
            var inputList = new InputBuilder().AddCharacters(text).ToArray();

            SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #24
0
        /// <summary>
        /// Simulates a modified keystroke where there is one modifier and multiple keys like CTRL-K-C where CTRL is the modifierKey and K and C are the keys.
        /// The flow is Modifier KeyDown, Keys Press in order, Modifier KeyUp.
        /// </summary>
        /// <param name="modifierKey">The modifier key</param>
        /// <param name="keyCodes">The list of keys to simulate</param>
        public void ModifiedKeyStroke(VirtualKeyCode modifierKey, IEnumerable <VirtualKeyCode> keyCodes)
        {
            var builder = new InputBuilder();

            builder.AddKeyDown(modifierKey);
            if (keyCodes != null)
            {
                keyCodes.ToList().ForEach(x => builder.AddKeyPress(x));
            }
            builder.AddKeyUp(modifierKey);

            SendSimulatedInput(builder.ToArray());
        }
Beispiel #25
0
        /// <summary>
        /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
        /// </summary>
        /// <param name="dikCode">The <see cref="DirectInputKeyCode"/> to press</param>
        /// <param name="delay">Delay in ms between keydown and keyup of final keyCode. 50ms should be minimum</param>
        public IKeyboardSimulator DelayedKeyPress(DirectInputKeyCode dikCode, int delay)
        {
            var inputList1 = new InputBuilder().AddKeyDown(dikCode).ToArray();

            SendSimulatedInput(inputList1);

            Thread.Sleep(delay);

            var inputList2 = new InputBuilder().AddKeyUp(dikCode).ToArray();

            SendSimulatedInput(inputList2);

            return(this);
        }
        private void ModifiersUp(InputBuilder builder, IEnumerable <VirtualKeyCode> modifierKeyCodes)
        {
            if (modifierKeyCodes == null)
            {
                return;
            }

            // Key up in reverse (I miss LINQ)
            var stack = new Stack <VirtualKeyCode>(modifierKeyCodes);

            while (stack.Count > 0)
            {
                builder.AddKeyUp(stack.Pop());
            }
        }
Beispiel #27
0
        /// <summary>
        /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS.
        /// </summary>
        /// <param name="keyCode">The <see cref="VirtualKeyCode"/> to press</param>
        public IKeyboardSimulator KeyPress(VirtualKeyCode keyCode)
        {
            unsafe
            {
                var input = stackalloc INPUT[2];
                input[0] = InputBuilder.MakeKeyDown(keyCode, _inputSimulator.ExtraInfo);
                input[1] = InputBuilder.MakeKeyUp(keyCode, _inputSimulator.ExtraInfo);

                _messageDispatcher.DispatchInput(input, 2);
            }

            //var inputList = new InputBuilder(){ ExtraInfo = _inputSimulator.ExtraInfo }.AddKeyPress(keyCode).ToArray();
            //SendSimulatedInput(inputList);
            return(this);
        }
Beispiel #28
0
        /// <summary>
        /// Simulates a modified keystroke where there are multiple modifiers and one key like CTRL-ALT-C where CTRL and ALT are the modifierKeys and C is the key.
        /// The flow is Modifiers KeyDown in order, Key Press, Modifiers KeyUp in reverse order.
        /// </summary>
        /// <param name="modifierKeyCodes">The list of modifier keys</param>
        /// <param name="keyCode">The key to simulate</param>
        public void ModifiedKeyStroke(IEnumerable <VirtualKeyCode> modifierKeyCodes, VirtualKeyCode keyCode)
        {
            var builder = new InputBuilder();

            if (modifierKeyCodes != null)
            {
                modifierKeyCodes.ToList().ForEach(x => builder.AddKeyDown(x));
            }
            builder.AddKeyPress(keyCode);
            if (modifierKeyCodes != null)
            {
                modifierKeyCodes.Reverse().ToList().ForEach(x => builder.AddKeyUp(x));
            }

            SendSimulatedInput(builder.ToArray());
        }
Beispiel #29
0
        /// <summary>
        /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate uninterrupted text entry via the keyboard.
        /// </summary>
        /// <param name="text">The text to be simulated.</param>
        public void TextEntry(string text)
        {
            if (text.Length > UInt32.MaxValue / 2)
            {
                throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");
            }

            //var chars = UTF8Encoding.Unicode.GetBytes(text);

            //var len = chars.Length;
            //INPUT[] inputList = new INPUT[len * 2];
            //for (int x = 0; x < len; x += 2)
            //{
            //    UInt16 scanCode = BitConverter.ToUInt16(chars, x);
            //}
            var inputList = new InputBuilder().AddCharacters(text).ToArray();

            SendSimulatedInput(inputList);
        }
        /// <summary>
        /// Simulates a modified keystroke where there are multiple modifiers and multiple keys like CTRL-ALT-K-C where CTRL and ALT are the modifierKeys and K and C are the keys.
        /// The flow is Modifiers KeyDown in order, Keys Press in order, Modifiers KeyUp in reverse order.
        /// </summary>
        /// <param name="modifierKeyCodes">The list of modifier keys</param>
        /// <param name="keyCodes">The list of keys to simulate</param>
        public void ModifiedKeyStroke(IEnumerable <VirtualKeyCode> modifierKeyCodes, IEnumerable <VirtualKeyCode> keyCodes)
        {
            var builder = new InputBuilder();
            var modifierKeyCodesList = modifierKeyCodes.ToList();

            if (modifierKeyCodes != null)
            {
                modifierKeyCodesList.ToList().ForEach(x => builder.AddKeyUp(x));
            }
            if (keyCodes != null)
            {
                keyCodes.ToList().ForEach(x => builder.AddKeyPress(x));
            }
            if (modifierKeyCodes != null)
            {
                modifierKeyCodesList.Reverse();
                modifierKeyCodesList.ForEach(x => builder.AddKeyUp(x));
            }

            SendSimulatedInput(builder.ToArray());
        }