KeyInjectionScope
-
Cmn
interface KeyInjectionScope : InjectionScope
The receiver scope of the key input injection lambda from performKeyInput.
All sequences and patterns of key input can be expressed using the two fundamental methods of this API - keyDown and keyUp. All other injection functions are provided as abstractions built on top of these two methods in order to improve test code readability/maintainability and decrease development time.
The entire event injection state is shared between all perform.*Input methods, meaning you can continue an unfinished key input sequence in a subsequent invocation of performKeyInput or performMultiModalInput.
All events sent by these methods are batched together and sent as a whole after performKeyInput has executed its code block.
When a key is held down - i.e. the virtual clock is forwarded whilst the key is pressed down, repeat key down events will be sent. In a fashion consistent with Android's implementation, the first repeat key event will be sent after a key has been held down for 500ms. Subsequent repeat events will be sent at 50ms intervals, until the key is released or another key is pressed down.
The sending of repeat key events is handled as an implicit side-effect of advanceEventTime, which is called within the injection scope. As such, no repeat key events will be sent if MainTestClock.advanceTimeBy is used to advance the time.
| See also | |
|---|---|
InjectionScope |
Summary
Public functions |
||
|---|---|---|
Boolean |
Checks if the given |
Cmn
|
Unit |
Sends a key down event for the given |
Cmn
|
Unit |
Sends a key up event for the given |
Cmn
|
Public properties |
||
|---|---|---|
Boolean |
Indicates whether caps lock is on or not. |
Cmn
|
Boolean |
Indicates whether num lock is on or not. |
Cmn
|
Boolean |
Indicates whether scroll lock is on or not. |
Cmn
|
Extension functions |
||
|---|---|---|
Unit |
KeyInjectionScope.pressKey(key: Key, pressDurationMillis: Long)Holds down the given |
Cmn
|
Unit |
KeyInjectionScope.withKeyDown(key: Key, block: KeyInjectionScope.() -> Unit)Executes the keyboard sequence specified in the given |
Cmn
|
Unit |
KeyInjectionScope.withKeyToggled(key: Key, block: KeyInjectionScope.() -> Unit)Executes the keyboard sequence specified in the given |
Cmn
|
Unit |
KeyInjectionScope.withKeysDown(keys: List<Key>, block: KeyInjectionScope.() -> Unit)Executes the keyboard sequence specified in the given |
Cmn
|
Unit |
KeyInjectionScope.withKeysToggled(keys: List<Key>, block: KeyInjectionScope.() -> Unit)Executes the keyboard sequence specified in the given |
Cmn
|
Extension properties |
||
|---|---|---|
Boolean |
Verifies whether either of the alt keys are down. |
Cmn
|
Boolean |
Verifies whether either of the control keys are down. |
Cmn
|
Boolean |
Verifies whether the function key is down. |
Cmn
|
Boolean |
Verifies whether either of the meta keys are down. |
Cmn
|
Boolean |
Verifies whether either of the shift keys are down. |
Cmn
|
Inherited functions |
|||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||
|
Inherited properties |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public functions
isKeyDown
fun isKeyDown(key: Key): Boolean
Checks if the given key is down.
| Parameters | |
|---|---|
key: Key |
The key to be checked. |
Public properties
isCapsLockOn
val isCapsLockOn: Boolean
Indicates whether caps lock is on or not.
Note that this reflects the state of the injected input only, it does not correspond to the state of an actual keyboard attached to the device on which a test is run
isNumLockOn
val isNumLockOn: Boolean
Indicates whether num lock is on or not.
Note that this reflects the state of the injected input only, it does not correspond to the state of an actual keyboard attached to the device on which a test is run
isScrollLockOn
val isScrollLockOn: Boolean
Indicates whether scroll lock is on or not.
Note that this reflects the state of the injected input only, it does not correspond to the state of an actual keyboard attached to the device on which a test is run
Extension functions
pressKey
fun KeyInjectionScope.pressKey(
key: Key,
pressDurationMillis: Long = DefaultKeyPressDurationMillis
): Unit
Holds down the given key for the given pressDurationMillis by sending a key down event, advancing the event time and sending a key up event.
If the given key is already down, an IllegalStateException will be thrown.
withKeyDown
fun KeyInjectionScope.withKeyDown(key: Key, block: KeyInjectionScope.() -> Unit): Unit
Executes the keyboard sequence specified in the given block, whilst holding down the given key. This key must not be used within the block.
If the given key is already down, an IllegalStateException will be thrown.
| Parameters | |
|---|---|
key: Key |
The key to be held down during injection of the |
block: KeyInjectionScope.() -> Unit |
Sequence of KeyInjectionScope methods to be injected with the given key down. |
withKeyToggled
fun KeyInjectionScope.withKeyToggled(key: Key, block: KeyInjectionScope.() -> Unit): Unit
Executes the keyboard sequence specified in the given block, in between presses to the given key. This key can also be used within the block, as long as it is not down at the end of the block.
If the given key is already down, an IllegalStateException will be thrown.
| Parameters | |
|---|---|
key: Key |
The key to be toggled around the injection of the |
block: KeyInjectionScope.() -> Unit |
Sequence of KeyInjectionScope methods to be injected with the given key down. |
withKeysDown
fun KeyInjectionScope.withKeysDown(keys: List<Key>, block: KeyInjectionScope.() -> Unit): Unit
Executes the keyboard sequence specified in the given block, whilst holding down the each of the given keys. Each of the keys will be pressed down and released simultaneously. These keys must not be used within the block.
If any of the given keys are already down, an IllegalStateException will be thrown.
| Parameters | |
|---|---|
keys: List<Key> |
List of keys to be held down during injection of the |
block: KeyInjectionScope.() -> Unit |
Sequence of KeyInjectionScope methods to be injected with the given keys down. |
withKeysToggled
fun KeyInjectionScope.withKeysToggled(keys: List<Key>, block: KeyInjectionScope.() -> Unit): Unit
Executes the keyboard sequence specified in the given block, in between presses to the given keys. Each of the keys will be toggled simultaneously.These keys can also be used within the block, as long as they are not down at the end of the block.
If any of the given keys are already down, an IllegalStateException will be thrown.
| Parameters | |
|---|---|
keys: List<Key> |
The keys to be toggled around the injection of the |
block: KeyInjectionScope.() -> Unit |
Sequence of KeyInjectionScope methods to be injected with the given keys down. |
Extension properties
isAltDown
val KeyInjectionScope.isAltDown: Boolean
Verifies whether either of the alt keys are down.
| Returns | |
|---|---|
Boolean |
true if an alt key is currently down, false otherwise. |
isCtrlDown
val KeyInjectionScope.isCtrlDown: Boolean
Verifies whether either of the control keys are down.
| Returns | |
|---|---|
Boolean |
true if a control key is currently down, false otherwise. |
isFnDown
val KeyInjectionScope.isFnDown: Boolean
Verifies whether the function key is down.
| Returns | |
|---|---|
Boolean |
true if the function key is currently down, false otherwise. |
isMetaDown
val KeyInjectionScope.isMetaDown: Boolean
Verifies whether either of the meta keys are down.
| Returns | |
|---|---|
Boolean |
true if a meta key is currently down, false otherwise. |
isShiftDown
val KeyInjectionScope.isShiftDown: Boolean
Verifies whether either of the shift keys are down.
| Returns | |
|---|---|
Boolean |
true if a shift key is currently down, false otherwise. |