Using the keyboard to send text to the clipboard

Apr 8, 2008 at 2:03 PM
Edited Apr 15, 2008 at 12:25 PM
I'm using the following code to try and send selected text to the clipboard using a CtrlC key combination but it doesnt send anything to the clipboard, am I using the keyboard object in the correct way to do a CtrlC keyboard operation?

Thanks,

James


(not the code extract can be used against the WPFTest app incldued in the white source distribution)
---
// get the text box and d a double click in the text box so thats something is selected
Core.UIItems.TextBox textBox = WPFTestMainWindow.Get<Core.UIItems.TextBox>(SearchCriteria.ByAutomationId("textBox"));
textBox.DoubleClick();

// use keyboard to do a CTRL+C
Keyboard keyB = Keyboard.Instance;
keyB.HoldKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);
keyB.Send("C");
keyB.LeaveKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);

//return clipboard contents to the console
System.Console.WriteLine("Text on clipboard is " + Clipboard.GetText());
Coordinator
Apr 10, 2008 at 2:09 AM
This seems to work for me.
Can you debug by putting a breakpoint after doubleclick and check whether the text in textbox is selected or not
Apr 10, 2008 at 11:00 AM
The double click is selecting the text; I already checked this as you suggested with a breakpoint.

Thanks,

James.
Coordinator
Apr 14, 2008 at 5:46 PM
Try doing doubleclick, (existing the program or making it break at that point). Manually press control-c and see if it works.
If you can provide me a failing test that would help a lot.
Apr 15, 2008 at 12:24 PM
Edited Apr 15, 2008 at 12:24 PM


viveksingh wrote:
Try doing doubleclick, (existing the program or making it break at that point). Manually press control-c and see if it works.
If you can provide me a failing test that would help a lot.


I placed a breakpoint on the line 'Keyboard keyB = Keyboard.Instance;' and confirmed at that point by pressing CtrlC by hand the text in the control gets placed on the clipboard. Text is selected in the control and the clipboard is empty before I press CtrlC by hand.

No matter how many times I run the following test, which I added to your WPFTestApp solution, its fails, nothing gets sent to the clipboard:

public void CopyTest()
{
// double click in textbox to give it the focus
Core.UIItems.TextBox textBox = WPFTestMainWindow.Get<Core.UIItems.TextBox>(SearchCriteria.ByAutomationId("textBox"));
textBox.DoubleClick();

// use keyboard to do a CTRL+C
Keyboard keyB = Keyboard.Instance;
keyB.HoldKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);
keyB.Send("C");
keyB.LeaveKey(Core.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);

//check the text is the same as that on the clipboard
string clipbrdText = Clipboard.GetText();
Assert.AreSame(textBox.Text, clipbrdText, "Text on clipboard does not match expected");
}

Can you try to add the above test to your WPFTestApp and confirm if it passes?

Thanks,

James.
Coordinator
Apr 15, 2008 at 2:36 PM
I ran the test for WPF application (earlier I ran for WinForm) but it still worked except that I used
Assert.AreEqual(textBox.Text, clipbrdText, "Text on clipboard does not match expected");
instead of AreSame.
Apr 16, 2008 at 11:25 AM
Edited Apr 16, 2008 at 11:26 AM


viveksingh wrote:
I ran the test for WPF application (earlier I ran for WinForm) but it still worked except that I used
Assert.AreEqual(textBox.Text, clipbrdText, "Text on clipboard does not match expected");
instead of AreSame.


Changing the assert makes no difference whatsoever, the assert is not the problem, its the key press code that does the Ctrl+C thats the problem; nothing is get sent to the clipboard.

This is beginning to worry me, how can you and I be getting different results using the same code?

I'm using the WPFTestApp solution contained in the latest version of the available source from this website(0.14) on a Windows XP machine. Are you trying this on the exact same 0.14 version or are you using your current latest version youre developing?

Thanks,

James.
Coordinator
Apr 16, 2008 at 12:27 PM
I found the issue finally. It was a timing issue. The reason is that the test is not waiting for the keyboard to finish the work.
Please do something like this:
AttachedKeyboard keyB = window.Keyboard;
keyB.HoldKey(KeyboardInput.SpecialKeys.CONTROL);
keyB.Enter("c");
keyB.LeaveKey(KeyboardInput.SpecialKeys.CONTROL);

The keyboard should be retrieved from window. This would cause it the test to wait till window is busy copying to clipboard.
Apr 16, 2008 at 3:00 PM


viveksingh wrote:
I found the issue finally. It was a timing issue. The reason is that the test is not waiting for the keyboard to finish the work.
Please do something like this:
AttachedKeyboard keyB = window.Keyboard;
keyB.HoldKey(KeyboardInput.SpecialKeys.CONTROL);
keyB.Enter("c");
keyB.LeaveKey(KeyboardInput.SpecialKeys.CONTROL);

The keyboard should be retrieved from window. This would cause it the test to wait till window is busy copying to clipboard.


That sounds promising, I'll try that this afternoon and get back to you.

Thanks for persisting with this for me,

James.
May 13, 2008 at 1:42 PM
Edited May 13, 2008 at 1:42 PM


jamesw wrote:


viveksingh wrote:
I found the issue finally. It was a timing issue. The reason is that the test is not waiting for the keyboard to finish the work.
Please do something like this:
AttachedKeyboard keyB = window.Keyboard;
keyB.HoldKey(KeyboardInput.SpecialKeys.CONTROL);
keyB.Enter("c");
keyB.LeaveKey(KeyboardInput.SpecialKeys.CONTROL);

The keyboard should be retrieved from window. This would cause it the test to wait till window is busy copying to clipboard.


That sounds promising, I'll try that this afternoon and get back to you.

Thanks for persisting with this for me,

James.


Sorry it took me so long to get back to you; the suggested code above does indeed work, thanks.

James.