Pause and resume media playback with external keyboard Spacebar
Stay organized with collections
Save and categorize content based on your preferences.
Whenever your app plays a media file, users should be able to pause and resume
playback by pressing the Spacebar on a physical keyboard.
Respond to keypress events
Apps based on Jetpack Compose or views respond to keyboard key presses in
similar ways: the app listens for keypress events, filters the events, and
responds to keypresses such as a Spacebar keypress.
1. Listen for keyboard events
Compose
With Jetpack Compose, use either the onPreviewKeyEvent or the
onKeyEvent modifier within the layout that manages the keystroke:
The method is invoked every time a pressed key is released, so it fires exactly
once for every keystroke.
2. Filter Spacebar presses
Inside the Compose onPreviewKeyEvent and onKeyEvent modifier methods or
views onKeyUp() method, filter for KeyEvent.KEYCODE_SPACE to send the
correct event to your media component:
onPreviewKeyEvent: Modifier that enables a component to intercept
hardware key events when it (or one of its children) is focused.
onKeyEvent: Similar to onPreviewKeyEvent, modifier that enables a
component to intercept hardware key events when the component (or one of its
children) is focused.
Views
onKeyUp(): Event handler called when a key is released and not handled
by a view (such as TextView) within an activity.
Results
Your app can now respond to Spacebar key presses to pause and resume
a video or other media.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-04 UTC."],[],[],null,["# Pause and resume media playback with external keyboard Spacebar\n\nWhenever your app plays a media file, users should be able to pause and resume\nplayback by pressing the \u003ckbd\u003eSpacebar\u003c/kbd\u003e on a physical keyboard.\n\nRespond to keypress events\n--------------------------\n\nApps based on Jetpack Compose or views respond to keyboard key presses in\nsimilar ways: the app listens for keypress events, filters the events, and\nresponds to keypresses such as a \u003ckbd\u003eSpacebar\u003c/kbd\u003e keypress.\n\n### 1. Listen for keyboard events\n\n**Compose**\n\nWith Jetpack Compose, use either the [`onPreviewKeyEvent`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).onPreviewKeyEvent(kotlin.Function1)) or the\n[`onKeyEvent`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).onKeyEvent(kotlin.Function1)) modifier within the layout that manages the keystroke: \n\n Column(modifier = Modifier.onPreviewKeyEvent { event -\u003e\n if (event.type == KeyEventType.KeyUp) {\n ...\n }\n ...\n })\n\nor \n\n Column(modifier = Modifier.onKeyEvent { event -\u003e\n if (event.type == KeyEventType.KeyUp) {\n ...\n }\n ...\n })\n\n| **Note:** The main difference between the two modifiers is where the event is dispatched if the modifier does not consume it: \n|\n| - `onPreviewKeyEvent` --- Dispatches the event to its first child\n| - `onKeyEvent` --- Dispatches the event to the composable's parent\n\n**Views**\n\nIn an activity in your app, override the [`onKeyUp()`](/reference/kotlin/android/app/Activity#onkeyup) method: \n\n### Kotlin\n\n```kotlin\noverride fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {\n ...\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onKeyUp(int keyCode, KeyEvent event) {\n ...\n}\n```\n\nThe method is invoked every time a pressed key is released, so it fires exactly\nonce for every keystroke.\n| **Caution:** Do not use the [`onKeyDown()`](/reference/kotlin/android/app/Activity#onkeydown) method, which fires repeatedly as long as the key is pressed.\n\n### 2. Filter \u003ckbd\u003eSpacebar\u003c/kbd\u003e presses\n\nInside the Compose `onPreviewKeyEvent` and `onKeyEvent` modifier methods or\nviews `onKeyUp()` method, filter for [`KeyEvent.KEYCODE_SPACE`](/reference/kotlin/android/view/KeyEvent#keycode_space) to send the\ncorrect event to your media component:\n\n**Compose** \n\n Column(modifier = Modifier.onPreviewKeyEvent { event -\u003e\n if (event.type == KeyEventType.KeyUp && event.key == Key.Spacebar) {\n ...\n }\n ...\n })\n\nor \n\n Column(modifier = Modifier.onKeyEvent { event -\u003e\n if (event.type == KeyEventType.KeyUp && event.key == Key.Spacebar) {\n ...\n }\n ...\n })\n\n**Views** \n\n### Kotlin\n\n```kotlin\nif (keyCode == KeyEvent.KEYCODE_SPACE) {\n togglePlayback()\n return true\n}\nreturn false\n```\n\n### Java\n\n```java\nif (keyCode == KeyEvent.KEYCODE_SPACE) {\n togglePlayback();\n return true;\n}\nreturn false;\n```\n| **Note:** Return `true` from the `onKeyUp()` method if your code manages the event and you don't want the event to propagate any further. Return `false` if you want to allow propagation of the event so that other components can manage the event.\n\nKey points\n----------\n\n- [`KEYCODE_SPACE`](/reference/kotlin/android/view/KeyEvent#keycode_space): Key code constant for the \u003ckbd\u003eSpacebar\u003c/kbd\u003e.\n\n**Compose**\n\n- [`onPreviewKeyEvent`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).onPreviewKeyEvent(kotlin.Function1)): Modifier that enables a component to intercept hardware key events when it (or one of its children) is focused.\n- [`onKeyEvent`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).onKeyEvent(kotlin.Function1)): Similar to `onPreviewKeyEvent`, modifier that enables a component to intercept hardware key events when the component (or one of its children) is focused.\n\n**Views**\n\n- [`onKeyUp()`](/reference/kotlin/android/app/Activity#onkeyup): Event handler called when a key is released and not handled by a view (such as [`TextView`](/reference/kotlin/android/widget/TextView)) within an activity.\n\n### Results\n\nYour app can now respond to \u003ckbd\u003eSpacebar\u003c/kbd\u003e key presses to pause and resume\na video or other media."]]