बाहरी कीबोर्ड के स्पेसबार का इस्तेमाल करके, मीडिया प्लेबैक को रोकना और फिर से शुरू करना
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
जब भी आपका ऐप्लिकेशन कोई मीडिया फ़ाइल चलाता है, तो उपयोगकर्ताओं को फ़िज़िकल कीबोर्ड पर Spacebar दबाकर, वीडियो को रोकने और फिर से चलाने की सुविधा मिलनी चाहिए.
बटन दबाने से होने वाले इवेंट का जवाब देना
Jetpack Compose या व्यू पर आधारित ऐप्लिकेशन, कीबोर्ड के बटन दबाने पर एक जैसे तरीके से जवाब देते हैं: ऐप्लिकेशन, बटन दबाने से जुड़े इवेंट सुनता है, इवेंट को फ़िल्टर करता है, और Spacebar बटन दबाने जैसे बटन दबाने पर जवाब देता है.
1. कीबोर्ड इवेंट को सुनना
Compose
Jetpack Compose में, कीस्ट्रोक को मैनेज करने वाले लेआउट में onPreviewKeyEvent या
onKeyEvent मॉडिफ़ायर का इस्तेमाल करें:
जब भी कोई बटन दबाया जाता है और छोड़ा जाता है, तब हर बार यह तरीका शुरू होता है. इसलिए, यह हर कीस्ट्रोक के लिए एक बार ट्रिगर होता है.
2. Spacebar दबाने की फ़्रीक्वेंसी को फ़िल्टर करना
अपने मीडिया कॉम्पोनेंट में सही इवेंट भेजने के लिए, लिखें onPreviewKeyEvent और onKeyEvent में बदलाव करने के तरीकों या व्यू onKeyUp() के तरीके में, KeyEvent.KEYCODE_SPACE के लिए फ़िल्टर करें:
KEYCODE_SPACE: Spacebar के लिए की कोड का कॉन्स्टेंट.
Compose
onPreviewKeyEvent: यह एक ऐसा मॉडिफ़ायर है जो किसी कॉम्पोनेंट को, हार्डवेयर के मुख्य इवेंट को इंटरसेप्ट करने की अनुमति देता है. ऐसा तब होता है, जब कॉम्पोनेंट या उसके किसी चाइल्ड पर फ़ोकस किया जाता है.
onKeyEvent: onPreviewKeyEvent की तरह ही, यह एक ऐसा मॉडिफ़ायर है जो कॉम्पोनेंट (या उसके किसी चाइल्ड) पर फ़ोकस होने पर, कॉम्पोनेंट को हार्डवेयर के मुख्य इवेंट को इंटरसेप्ट करने की अनुमति देता है.
व्यू
onKeyUp(): इवेंट हैंडलर तब कॉल किया जाता है, जब कोई बटन छोड़ा जाता है और किसी गतिविधि में मौजूद किसी व्यू (जैसे, TextView) से उसे मैनेज नहीं किया जाता.
नतीजे
अब आपका ऐप्लिकेशन, Spacebar बटन को दबाने पर, वीडियो या अन्य मीडिया को रोक सकता है और फिर से चला सकता है.
इस पेज पर मौजूद कॉन्टेंट और कोड सैंपल कॉन्टेंट के लाइसेंस में बताए गए लाइसेंस के हिसाब से हैं. 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."]]