English | Español
A clean Android sample app that shows how to implement keyboard shortcuts in a modern Jetpack Compose app.
This project is meant to be a small public reference for Android developers who want to support:
- app-level keyboard shortcuts
- screen-level keyboard shortcuts
- Android Keyboard Shortcuts Helper integration
- a single-Activity architecture with Navigation Compose
The sample uses the official Android APIs for keyboard shortcuts:
Activity.onKeyShortcut(...)to execute shortcutsActivity.onProvideKeyboardShortcuts(...)to publish shortcuts to the system helperActivity.requestShowKeyboardShortcuts()to open the helper programmatically
Compose screens register their local shortcuts through a shared registry. The same registry is also used to build KeyboardShortcutGroup objects for the Android system helper.
- A single-Activity Compose app with Navigation Compose
- Global app shortcuts that work from any screen
- Local screen shortcuts that are active only while a screen is visible
- Screen shortcuts taking precedence over app shortcuts when the same key combination is reused
- Shortcut execution and system discoverability driven by the same definitions
Ctrl + 1-> Go to Blue screenCtrl + 2-> Go to Green screenCtrl + 0-> Go to Home
Ctrl + B-> Toggle blue shadeCtrl + H-> Go Home
Ctrl + G-> Toggle green shadeCtrl + H-> Go Home
Many keyboard shortcut examples stop at raw key handling. This sample focuses on the more complete Android story:
- executing shortcuts with official APIs
- publishing shortcuts to Android Keyboard Shortcuts Helper
- keeping global and screen-specific shortcuts organized in a readable structure
- avoiding restricted APIs and unnecessary workarounds
The app keeps the structure intentionally small and explicit:
com.haynlo.keyboardshortcuts
├── MainActivity.kt
├── navigation
│ ├── AppDestinations.kt
│ └── AppNavHost.kt
├── shortcuts
│ └── ShortcutRegistry.kt
└── ui
├── BlueScreen.kt
├── GreenScreen.kt
├── HomeScreen.kt
├── common
│ └── DemoScreen.kt
├── registration
│ ├── AppShortcutsEffect.kt
│ └── ScreenShortcutsEffect.kt
└── theme
├── Color.kt
└── Theme.kt
MainActivity is the bridge between Android and Compose:
onKeyShortcut(...)asks the registry to resolve and execute the active shortcutonProvideKeyboardShortcuts(...)asks the registry to publish the active shortcut groups
AppShortcutsEffect registers global navigation shortcuts from AppNavHost.
ScreenShortcutsEffect registers screen-specific shortcuts from each screen.
When a screen leaves composition, its local shortcuts are removed automatically.
ShortcutRegistry resolves screen groups before app groups, so local shortcuts win when both match the same combination.
For simple key handling inside a focused composable, Modifier.onKeyEvent can be enough.
This sample focuses on a different use case: discoverable shortcuts that should be integrated with Android Keyboard Shortcuts Helper. For that reason, it uses Activity.onKeyShortcut(...) and onProvideKeyboardShortcuts(...) instead.
- Android Studio
- Android SDK
- minSdk
25
./gradlew build- UI strings are available in English and Spanish.
- The sample keeps the UI intentionally minimal so the shortcut behavior stays easy to follow.
- The project avoids
dispatchKeyShortcutEvent()and does not use@SuppressLint("RestrictedApi").
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
