示例#1
0
        /// <summary>
        /// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.
        /// </summary>
        /// <param name="text">A text to type into a focused element</param>
        /// <param name="options">type options</param>
        /// <remarks>
        /// To press a special key, like <c>Control</c> or <c>ArrowDown</c>, use <see cref="PressAsync(string, PressOptions)"/>
        /// </remarks>
        /// <returns>Task</returns>
        public async Task TypeAsync(string text, TypeOptions options = null)
        {
            var delay = 0;

            if (options?.Delay != null)
            {
                delay = (int)options.Delay;
            }

            var textParts = StringInfo.GetTextElementEnumerator(text);

            while (textParts.MoveNext())
            {
                var letter = textParts.Current;
                if (KeyDefinitions.ContainsKey(letter.ToString()))
                {
                    await PressAsync(letter.ToString(), new PressOptions { Delay = delay }).ConfigureAwait(false);
                }
                else
                {
                    if (delay > 0)
                    {
                        await Task.Delay(delay).ConfigureAwait(false);
                    }
                    await SendCharacterAsync(letter.ToString()).ConfigureAwait(false);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.
        /// </summary>
        /// <param name="text">A text to type into a focused element</param>
        /// <param name="options">type options</param>
        /// <remarks>
        /// To press a special key, like <c>Control</c> or <c>ArrowDown</c>, use <see cref="PressAsync(string, PressOptions)"/>
        /// </remarks>
        /// <returns>Task</returns>
        public async Task TypeAsync(string text, TypeOptions options = null)
        {
            var delay = 0;

            if (options?.Delay != null)
            {
                delay = (int)options.Delay;
            }
            foreach (var letter in text)
            {
                if (KeyDefinitions.ContainsKey(letter.ToString()))
                {
                    await PressAsync(letter.ToString(), new PressOptions { Delay = delay });
                }
                else
                {
                    await SendCharacterAsync(letter.ToString());
                }
                if (delay > 0)
                {
                    await Task.Delay(delay);
                }
            }
        }
示例#3
0
        private KeyDefinition KeyDescriptionForString(string keyString)
        {
            var shift       = Modifiers & 8;
            var description = new KeyDefinition
            {
                Key      = string.Empty,
                KeyCode  = 0,
                Code     = string.Empty,
                Text     = string.Empty,
                Location = 0
            };

            var definition = KeyDefinitions.Get(keyString);

            if (!string.IsNullOrEmpty(definition.Key))
            {
                description.Key = definition.Key;
            }

            if (shift > 0 && !string.IsNullOrEmpty(definition.ShiftKey))
            {
                description.Key = definition.ShiftKey;
            }

            if (definition.KeyCode > 0)
            {
                description.KeyCode = definition.KeyCode;
            }

            if (shift > 0 && definition.ShiftKeyCode != null)
            {
                description.KeyCode = (int)definition.ShiftKeyCode;
            }

            if (!string.IsNullOrEmpty(definition.Code))
            {
                description.Code = definition.Code;
            }

            if (definition.Location != 0)
            {
                description.Location = definition.Location;
            }

            if (description.Key.Length == 1)
            {
                description.Text = description.Key;
            }

            if (!string.IsNullOrEmpty(definition.Text))
            {
                description.Text = definition.Text;
            }

            if (shift > 0 && !string.IsNullOrEmpty(definition.ShiftText))
            {
                description.Text = definition.ShiftText;
            }

            // if any modifiers besides shift are pressed, no text should be sent
            if ((Modifiers & ~8) > 0)
            {
                description.Text = string.Empty;
            }

            return(description);
        }