Skip to content

Instantly share code, notes, and snippets.

@MwBakker
Created April 22, 2020 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MwBakker/42e5a9c3f30a883555dd5deb5c4a79ca to your computer and use it in GitHub Desktop.
Save MwBakker/42e5a9c3f30a883555dd5deb5c4a79ca to your computer and use it in GitHub Desktop.
public async Task<bool> PerformTextualInputAsync(string text)
{
UIElement focusedControl = Keyboard.FocusedElement as UIElement;
if (focusedControl != null)
{
text = text.ToUpper(System.Globalization.CultureInfo.CurrentCulture);
string[] textArray = text.ToCharArray().Select(c => c.ToString()).ToArray();
if (Keyboard.PrimaryDevice.ActiveSource != null)
{
// deadlock
await Task.Run(() => PerformAllKeyBoardInput(textArray));
// check if value is set
if (((TextBox)focusedControl).Text != null)
{
return ((TextBox)focusedControl).Text.Equals(text) ? true : false;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
private void PerformAllKeyBoardInput(string[] textArray)
{
System.Windows.Forms.Keys key;
foreach (string element in textArray)
{
Enum.TryParse(element, out key);
PressKey(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment