Skip to main content

Key Event

The Key Event action simulates pressing a hardware or system key on the Android device using its key code. This action can be used to trigger standard Android key events that are not covered by dedicated automation methods.

When to Use

Use this action when you want to:

  • Simulate pressing the Enter key.
  • Trigger the Menu key.
  • Control media playback.
  • Perform other supported Android key events.

Syntax

var task = suremdmjs.automate().keyEvent(
<Android Key Code>
);

suremdmjs.automate().performAction(
task,
"onSuccess",
"onFailure"
);

Parameters

ParameterRequiredDescription
Android Key CodeYesThe Android key code representing the hardware or system key to simulate.

Parameter Details

Some commonly used Android key codes include:

KeyKey Code
Enter66
Tab61
Space62
Delete (Backspace)67
Escape111
Menu82
note

The complete list of supported Android key codes is defined by the Android framework. Refer to the official Android KeyEvent documentation for all available values.

Example

The following example simulates pressing the Enter key.

!#suremdmjs
pressEnter();

function pressEnter() {
var task = suremdmjs.automate().keyEvent(66);
suremdmjs.automate().performAction(
task,
"keyPressed",
"keyPressFailed"
);
}

function keyPressed() {
suremdmjs.log("Enter key pressed.");
}

function keyPressFailed() {
suremdmjs.log("Unable to send key event.");
}

Expected Result

The specified Android key event is sent to the currently active application.

💬 Help us improve this documentation

Was this information useful?

Your feedback helps us keep our documentation accurate, up to date, and useful.