Daily automation: SendKeys tricks


Today’s comment is absolutely worth posting here!

How to send a special character via SendKeys – it may be not so easy as I thought.

First of all, how to do this task in an easy way (i.e., using ValuePattern). I created a simple Windows Forms application called ‘testeditbox’ with a label and a text box (the label is not used in the test, just a typical case text box + label).

SendKeys001
We can write in and, after, clean it the following way:

Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAEditText texttexttext;

SendKeys002

Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAEditText "";

SendKeys003

Now, we are going to perform this without using the pattern. There are two ways to put text in: Set-UIAControlText and Set-UIAControlKeys.
The first method can’t send special characters (it’s just my fault, I need to add the possibility), it works through the SendMessage API call. It should work pretty well as it sends to a handle.
The second is .NET SendKeys.
The problem is that SendKeys sends only to the focused control. Well, I achieved the result this way:

Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAFocus;
Get-UIAEdit | Set-UIAControlKeys "text2";

SendKeys004

Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAFocus;
Get-UIAEdit | Set-UIAControlKeys "^(A){DELETE}";

SendKeys005

Step by step guide:
1) get a control
2) set focus
3) get a control again (the Set-UIAControlKeys requires input), the control is foreground now
4) put keys to the control (Ctrl+A and Delete).

If your control of some other sort, you may need to do other tricks. Nonetheless, the idea behind is as I demonstrated.

Advertisements

3 responses

  1. Hi Alexander,
    thanks for such a detailed response! (WordPress don’t notified me of that post via email)
    “Send special characters” possibility would be helpful. To set focus to a control (and it means to set visible its form) at this case “is not comme il faut”, i guess.
    I’ve a new question (about delphi classes.):
    Get-UIAPane -Name ‘Cancel’ returns one object (that button). But
    Get-UIAPane -Class ‘TTeButton’ -Name ‘Cancel’ returns all buttons of the form.
    Why?
    Sorry for my poor English (Могу на нашем написать :))

    1. Hello Alex,
      if you prefer our local language, I’d suggest using a special e-mail address: uiautomation(AT)hotmail(DOT)com. I’ll convert all interesting topics conversed between us into blog posts.
      In principle, I could also accept a skype call or visit a test host via RDP, or at AWS.

      Regarding your code sample, you’ve found a bug! I suspected that something is wrong with search (this project (UIA) does not have unit tests due to difficulty with a class that derives from System.Object and could not be inherited (thanks, Microsoft!). I’m planning to start writing unit tests with a wrapper but lacked time).
      A Calculator sample:
      Get-UIAWindow -pn calc | Get-UIAButton -Name ‘Clear’ # one element
      Get-UIAWindow -pn calc | Get-UIAButton -Class ‘Button’ -Name ‘Clear’ # a number of elements

    2. Hello Alex,
      just to inform you, I fixed the bug ‘too rich output if a user selected the -Class parameter’ in 0.8.4.
      I even re-wrote the ‘selection of elements by a parameter(s)’ to LINQ (it should also be approx. faster as much as twice, i.e. take less milliseconds :)).

      In 0.8.4, I need to admit, I re-invented the bug with the brand-new -Value parameter (the parameter enables us to search for controls by text in an Edit field or the label upon a grid cell). I’ll fix this new one soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: