Skip to end of metadata
Go to start of metadata

All FEST Fixtures can simulate user input using a keyboard. This simulation is not limited to send events to KeyListeners. FEST provides true keyboard input, at the operating system level.

Pressing and Releasing One or More Keys

The method pressAndReleaseKeys(int...) can simulate a user pressing and releasing one or more keys. If more than one key code is passed as parameter, this method will simulate pressing a key, releasing it and then will move to the next key code. Key codes are integers specified in java.awt.event.KeyEvent.

The following example shows how to simulate a user pressing the keys "A," "S," "D," "F" while having input focus on a text field with name "username":

which is similar to typing "asdf" on that text field.

The following example shows how to simulate a user pressing the key "F1" while having focus on a button with name "configure":

Pressing a Key, Doing Something Else, And Releasing the Key

The methods pressKey(int) and releaseKey(int) can simulate a user pressing and releasing a key. Although it may sound similar to what the method pressAndReleaseKeys(int...) can do, it is quite different. The pressAndReleaseKeys(int...) simulates a user pressing a key and immediately releasing the same key. On the other hand, pressKey(int) and releaseKey(int) are two separate actions. They can be helpful when we want to simulate a case of "press a key, do something else, and then release the key."

An example will help us have a better idea what these methods are good for. In the following code listing we are simulating clicking the left mouse button while having the "Ctrl" key pressed: