Tag Archives: SendKeys

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
%d bloggers like this: