コード例 #1
0
        /// <inheritdoc cref="IKeyboard.TypeAsync(string, int)"/>
        public async Task TypeAsync(string text, int delay = 0)
        {
            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
        private KeyDescription GetKeyDescriptionForString(string keyString)
        {
            if (!KeyDefinitions.ContainsKey(keyString))
            {
                throw new PlaywrightSharpException($"Unknown key: \"{keyString}\"");
            }

            bool shift       = _pressedModifiers.Contains(Modifier.Shift);
            var  description = new KeyDescription
            {
                Key     = string.Empty,
                KeyCode = 0,
                KeyCodeWithoutLocation = 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 && !string.IsNullOrEmpty(definition.ShiftKey))
            {
                description.Key = definition.ShiftKey;
            }

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

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

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

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

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

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

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

            // if any modifiers besides shift are pressed, no text should be sent
            if (_pressedModifiers.Count > 1 || (!shift && _pressedModifiers.Count == 1))
            {
                description.Text = string.Empty;
            }

            if (definition.KeyCodeWithoutLocation != null)
            {
                description.KeyCodeWithoutLocation = definition.KeyCodeWithoutLocation.Value;
            }
            else
            {
                description.KeyCodeWithoutLocation = definition.KeyCode.GetValueOrDefault();
            }

            return(description);
        }