EzKeyboardFunctions
EzKeyboardFunctions is one of two hardware manipulation classes (the other being EzMouseFunctions) that will physically send keyboard input to the application under test
💡 Note
Using this class will physically control your keyboard. Be sure that you computer is in a state where you do not need to use it for anything else except automation
Syntax
public static class EzKeyboardFunctions
Methods
Name | Return Type | Description |
---|---|---|
PressKey(EzElement element, Key key) | void | Presses the keyboard key on the element passed in |
SendText(EzElement element, string text) | void | Sends the string passed in as keyboard intput to the EzElement instance passed in |
SendTextWithWait(EzElement element, string text, int? wait = null) | void | Sends the string passed in as keyboard intput to the EzElement instance passed in with a wait between each character in the string |
RepeatKey(EzElement element, int repeatNumber, Key key) | void | Repeats a key press based on the repeatNumber int passed in |
ShiftCombination(EzElement element, params Key[] keys) | void | Sends each key in the keys object to the element while pressing the Shift key |
ShiftCombination(EzElement element, string keys) | void | Sends the keys string to the element while pressing the Shift key |
CtrlCombination(EzElement element, params Key[] keys) | void | Sends each key in the keys object to the element while pressing the Ctrl key |
CtrlCombination(EzElement element, string keys) | void | Sends the keys string to the element while pressing the Ctrl key |
AltCombination(EzElement element, params Key[] keys) | void | Sends each key in the keys object to the element while pressing the Alt key |
AltCombination(EzElement element, string keys) | void | Sends the keys string to the element while pressing the Alt key |
Samples and Usage
The EzKeyboardFunctions class will physically send text to the application under test. You can either send a string to the application or you can send an individual key. An individual key is helpful if you need to send something like an arror key or the backspace button for example.
Sample:
//Simple method to launch Windows' built in notepad application const string notepadPath = C:\\Windows\\System32\\notepad.exe"; //Get an instance of EzProcess in order to create EzRoot. Wrap it in a using statement to ensure that //.Dispose() is called using (EzProcess process = new EzProcess(notepadPath, "Notepad")) { process.StartProcess(); //Get the root element of the application EzRoot root = new EzRoot(process); //Query the root element for the first child whose name is "Text Editor". This is the main text area //of notepad EzElement textEditor = root.RootElement.FindChildByName("Text Editor"); //Send text to notepad.exe. Use the SendWithWait method which will simulate a gap inbetween letters //as ifsomeone is actually typing EzKeyboardFunctions.SendTextWithWait(textEditor, "This is a test of my new automation code. " + "This text will be written to the notepad instance that I have launched."); //Use the RepeatKey method to hit the Enter key two times EzKeyboardFunctions.RepeatKey(textEditor, 2, Key.Enter); //Use the SendText method to send some text with no wait. It appears much more quickly EzKeyboardFunctions.SendText(textEditor, "This text is sent with no wait. " + "It will appear much more quickly"); }
This is the result of the above code being run: