השהיה והמשך של הפעלת מדיה באמצעות מקש הרווח במקלדת חיצונית
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
בכל פעם שהאפליקציה מפעילה קובץ מדיה, המשתמשים צריכים להיות מסוגלים להשהות ולהמשיך את ההפעלה בלחיצה על מקש הרווח במקלדת פיזית.
תגובה לאירועי הקשה על מקש
אפליקציות שמבוססות על Jetpack Compose או על תצוגות מגיבים ללחיצות על מקשי המקלדת בדרכים דומות: האפליקציה מקשיבה לאירועי הקשה על מקש, מסננת את האירועים ומגיבה ללחיצות על מקשים כמו הקשה על מקש הרווח.
onPreviewKeyEvent: משתנה משתנה שמאפשר לרכיב ליירט אירועי מפתח של חומרה כשהוא (או אחד הצאצאים שלו) נמצא בחזית.
onKeyEvent: בדומה ל-onPreviewKeyEvent, שינוי שמאפשר לרכיב ליירט אירועי מפתחות של חומרה כשהרכיב (או אחד מהצאצאים שלו) נמצא בחזית.
תצוגות מפורטות
onKeyUp(): גורם מטפל באירועים שנקרא כשמקש משוחרר ולא מטופל על ידי תצוגה (כמו TextView) בתוך פעילות.
תוצאות
מעכשיו האפליקציה יכולה להגיב ללחיצות על מקש הרווח כדי להשהות ולהמשיך סרטון או מדיה אחרת.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-27 (שעון 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."]]