Simulating Keystrokes

Applies to TestLeft 4.72, last modified on December 21, 2020

Sometimes during testing you need to send a sequence of pressed buttons (a keystroke) to the specified object - a control or a window.

You can simulate keyboard input from your tests by using the following methods:

  • IControl.Keys - Sends keystrokes to the control to which the interface provides access.

  • IDesktop.Keys - Sends keystrokes to the window or control that is currently focused.

  • Control.keys - Sends keystrokes to the control to which the object provides access.

  • Desktop.keys - Sends keystrokes to the window or control that is currently focused.

The methods have one parameter - a string of the keys to simulate. The string can include alphanumeric characters, as well as special key codes (Ctrl, Alt, F1, Tab and others).

Note: You can use the ITextEdit.SetText method (in .NET) or the TextEdit.setwText method (in Java) to simulate entering text in an edit control. The method takes less time to simulate keystrokes than the Keys method.

However, the Keys method simulates actual keystrokes. The method also ensures that the appropriate keyboard events are raised in the tested application and the associated event handlers (if any) are executed.

Key Constants

The Keys method takes a parameter that specifies the keystroke to simulate.

To simulate pressing system keys, such as Ctrl, Alt, Shift, F1, Tab and so on, use the following constants:

Constant Simulated Key
^ Ctrl
! Shift
~ Alt
^^ ^
!! !
~~ ~
[[ [
[Apps] Application key
[BS] Backspace
[Caps] Caps Lock
[Clear] Clear
[Del] Delete
[Down] Down
[End] End
[Enter] Enter
[Esc] Esc
[F1] F1
[F2] F2
[F3] F3
[F4] F4
[F5] F5
[F6] F6
[F7] F7
[F8] F8
[F9] F9
[F10] F10
[F11] F11
[F12] F12
Constant Simulated Key
[Home] Home
[Ins] Insert
[Left] Left
[NumAsterisk] Num * (* on the numeric keyboard)
[NumLock] Num Lock
[NumMinus] Num - (- on the numeric keyboard)
[NumPlus] Num + (+ on the numeric keyboard)
[NumSlash] Num / (/ on the numeric keyboard)
[PageDown] PageDown
[PageUp] PageUp
[Pause] Pause
[PrtSc] PrintScreen
[Right] Right
[ScrollLock] Scroll Lock
[Tab] Tab
[Up] Up
[Win] Windows key
[Pnnn] Pauses typing for nnn milliseconds.
[Xnn] Specifies a key by its virtual-key code. nn is the decimal virtual-key code of the desired key. For example, [X107] corresponds to the Num Plus key. To learn more about virtual-key codes, see the Virtual-Key Codes article in the MSDN Library.
[Dnn] Presses a dead key specified by its key code nn.
[Altnn…] Simulates pressing Alt and typing numeric keys on the numeric keypad (that is, Alt+the character code). nn… is a sequence of numeric keys to be pressed, for instance, [Alt119] will simulate pressing Alt and typing the 1, 1 and 9 keys on the numeric keypad.
All constants (F1, PageDown, X, P, etc.) are case-sensitive.

Shift codes (^, ~ and !) parameter do not press Ctrl, Alt or Shift. Instead, the next key in the string will be pressed under the indicated shift conditions. That is, the shift codes influence on the first non-shift key that follows them. For instance, the following code presses Ctrl+ESC (this shortcut shows the Start menu):

C#

using SmartBear.TestLeft;

Driver driver = new LocalDriver;
driver.Desktop.Keys("^[Esc]");

Visual Basic .NET

Imports SmartBear.TestLeft;

Dim driver As IDriver = New LocalDriver
driver.Desktop.Keys("^[Esc]")

Java

import com.smartbear.testleft.*;

Driver driver = new LocalDriver();
driver.getDesktop().keys("^[Esc]");

To separate shift-key presses from combined key presses like the above, call the Keys method separately.

To simulate simultaneous pressing of several keys, use the [Hold] constant. For instance, [Hold][Win]e will launch Windows Explorer.

To deactivate [Hold], use the [Release] and [ReleaseLast] constants. [ReleaseLast] commands TestLeft to simulate the releasing of the last pressed key. For instance, the string [Hold]^f[ReleaseLast]U corresponds to the following sequence of keypresses: pressing of Ctrl, pressing of f, releasing of f, pressing of U, releasing of U and Ctrl.

[Release] simulates the release of all pressed keys. For example, the string [Hold]^fU[Release] (or [Hold]^fU) commands TestLeft to simulate pressing Ctrl, then pressing f, then pressing U and then releasing all these keys.

Native-Language Characters

The Key methods recognize native-language characters and automatically switches the keyboard layout to enter these characters.

To simulate native-language keystrokes, you can also use the [Xnn] or [Dnn]constants in the string passed to the Keys method. nn specifies the virtual-key code of the key to be “pressed”. For instance, [X221]u[X221]e will “type” the u and e characters with the circumflex accent.

The difference between X and D is that D simulates pressing of dead-key characters while X simulates pressing of any key by its virtual-key code. When you specify the D tag, TestLeft checks whether the specified dead-key character is supported by languages installed in the operating system. If the check is successful, TestLeft activates the appropriate keyboard layout and simulates the specified key press.

Another way to simulate typing of native-language or special characters is to use the [Altnn] constants. This constant commands TestLeft to simulate the Alt key press and typing of numeric keys on the numeric keypad (that is, ALT+character_code on the numeric keypad, a standard way to enter characters by their ANSI character code).

To “type” native-language characters into rich edit controls, you can use the shortcut supports by these controls (this works with English, French, German, Italian, and Spanish keyboards only):

Shortcut Description Example Result
Ctrl+' (apostrophe) + the desired character Accent acute [Hold]^'a
Ctrl+` (grave) + the desired character Accent grave [Hold]^`a
Ctrl+~ (tilde) + the desired character Accent tilde [Hold]^~~a
Ctrl+; (semicolon) + the desired character Accent umlaut [Hold]^;a
Ctrl+Shift+6 + the desired character Accent circumflex [Hold]^!6a
Ctrl+, (comma) + c or C Accent cedilla [Hold]^,c
Example

The following sample code simulates pressing the Ctrl+N key combination in the Notepad application (Notepad must be running):

C#

using SmartBear.TestLeft;
using SmartBear.TestLeft.TestObjects;


public void Test()
{
  ITopLevelWindow wndNotepad = Driver.Find<IProcess>(new ProcessPattern()
  {
    ProcessName = "notepad"
  }).Find<ITopLevelWindow>(new WindowPattern()
  {
    WndClass = "Notepad"
  });

  wndNotepad.Keys("^n");
}

Visual Basic .NET

Imports SmartBear.TestLeft
Imports SmartBear.TestLeft.TestObjects


Public Sub Test()
  Dim wndNotepad As ITopLevelWindow = Driver.Find(Of IProcess)(New ProcessPattern() With {
          .ProcessName = "notepad"
  }).Find(Of ITopLevelWindow)(New WindowPattern() With {
          .WndClass = "Notepad"
  })
  wndNotepad.Keys("^n")
End Sub

Java

import com.smartbear.testleft.*;
import com.smartbear.testleft.testobjects.*;


@Test
public void Test() throws Exception{

  TopLevelWindow wndNotepad = driver.find(TestProcess.class, new ProcessPattern() {{
    ProcessName = "notepad";
  }}).find(TopLevelWindow.class, new WindowPattern() {{
    WndClass = "Notepad";
  }});
  wndNotepad.keys("^n");
}

Remarks
  • To simulate capital letters, the Keys method “presses” Shift tacitly. That is, the simulation of tEst is performed by keystrokes of the following: t, Shift-e, s and t.

  • The keystroke simulation ignores the current state of Caps Lock. The method “types” the text as it is specified by the Keys parameter regardless of whether the Caps Lock mode is active or not. For instance, if you pass the string Test and Caps Lock is active, the method will simulate typing of the string Test rather than TEST.

  • TestLeft uses a slower keystroke simulation speed for controls that reside on web pages shown in Internet Explorer.

  • When creating or debugging tests, it maybe difficult to understand the state of the tested window before simulating keystrokes. You can enable TestLeft Visualizer in your test to capture images of the tested windows and controls automatically.

See Also

Simulating User Actions
Creating TestLeft Tests
About Driver Objects
Object Identification

Highlight search results