private void KeysPress(InputBuilder builder, IEnumerable <VirtualKeyCode> keyCodes)
 {
     if (keyCodes == null)
     {
         return;
     }
     foreach (var key in keyCodes)
     {
         builder.AddKeyPress(key);
     }
 }
 private void ModifiersDown(InputBuilder builder, IEnumerable <VirtualKeyCode> modifierKeyCodes)
 {
     if (modifierKeyCodes == null)
     {
         return;
     }
     foreach (var key in modifierKeyCodes)
     {
         builder.AddKeyDown(key);
     }
 }
        /// <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();

            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(
                          string.Format("The text parameter is too long. It must be less than {0} characters.",
                                        uint.MaxValue / 2), "text");
            }
            var inputList = new InputBuilder().AddCharacters(text).ToArray();

            SendSimulatedInput(inputList);
            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());
            }
        }