|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.compose.snippets.touchinput.keyboardinput |
| 18 | + |
| 19 | +import android.app.Activity |
| 20 | +import android.os.Build |
| 21 | +import android.os.Bundle |
| 22 | +import android.view.KeyEvent |
| 23 | +import android.view.KeyboardShortcutGroup |
| 24 | +import android.view.KeyboardShortcutInfo |
| 25 | +import android.view.Menu |
| 26 | +import androidx.activity.ComponentActivity |
| 27 | +import androidx.activity.compose.setContent |
| 28 | +import androidx.activity.enableEdgeToEdge |
| 29 | +import androidx.annotation.RequiresApi |
| 30 | +import androidx.compose.material3.Button |
| 31 | +import androidx.compose.material3.MaterialTheme |
| 32 | +import androidx.compose.material3.Text |
| 33 | +import androidx.compose.ui.platform.LocalContext |
| 34 | + |
| 35 | +class MainActivity : ComponentActivity() { |
| 36 | + // Activity codes such as overridden onStart method. |
| 37 | + |
| 38 | + @RequiresApi(Build.VERSION_CODES.N) |
| 39 | + // [START android_compose_keyboard_shortcuts_helper] |
| 40 | + override fun onProvideKeyboardShortcuts( |
| 41 | + data: MutableList<KeyboardShortcutGroup>?, |
| 42 | + menu: Menu?, |
| 43 | + deviceId: Int |
| 44 | + ) { |
| 45 | + val shortcutGroup = |
| 46 | + KeyboardShortcutGroup( |
| 47 | + "Cursor movement", |
| 48 | + listOf( |
| 49 | + KeyboardShortcutInfo("Up", KeyEvent.KEYCODE_P, KeyEvent.META_CTRL_ON), |
| 50 | + KeyboardShortcutInfo("Down", KeyEvent.KEYCODE_N, KeyEvent.META_CTRL_ON), |
| 51 | + KeyboardShortcutInfo("Forward", KeyEvent.KEYCODE_F, KeyEvent.META_CTRL_ON), |
| 52 | + KeyboardShortcutInfo("Backward", KeyEvent.KEYCODE_B, KeyEvent.META_CTRL_ON), |
| 53 | + ) |
| 54 | + ) |
| 55 | + data?.add(shortcutGroup) |
| 56 | + } |
| 57 | + // [END android_compose_keyboard_shortcuts_helper] |
| 58 | +} |
| 59 | + |
| 60 | +class AnotherActivity : ComponentActivity() { |
| 61 | + |
| 62 | + @RequiresApi(Build.VERSION_CODES.N) |
| 63 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 64 | + super.onCreate(savedInstanceState) |
| 65 | + enableEdgeToEdge() |
| 66 | + setContent { |
| 67 | + MaterialTheme { |
| 68 | + // [START android_compose_keyboard_shortcuts_helper_request] |
| 69 | + val activity = LocalContext.current as? Activity |
| 70 | + |
| 71 | + Button( |
| 72 | + onClick = { |
| 73 | + activity?.requestShowKeyboardShortcuts() |
| 74 | + } |
| 75 | + ) { |
| 76 | + Text(text = "Show keyboard shortcuts") |
| 77 | + } |
| 78 | + // [END android_compose_keyboard_shortcuts_helper_request] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @RequiresApi(Build.VERSION_CODES.N) |
| 84 | + // [START android_compose_keyboard_shortcuts_helper_with_groups] |
| 85 | + override fun onProvideKeyboardShortcuts( |
| 86 | + data: MutableList<KeyboardShortcutGroup>?, |
| 87 | + menu: Menu?, |
| 88 | + deviceId: Int |
| 89 | + ) { |
| 90 | + val cursorMovement = KeyboardShortcutGroup( |
| 91 | + "Cursor movement", |
| 92 | + listOf( |
| 93 | + KeyboardShortcutInfo("Up", KeyEvent.KEYCODE_P, KeyEvent.META_CTRL_ON), |
| 94 | + KeyboardShortcutInfo("Down", KeyEvent.KEYCODE_N, KeyEvent.META_CTRL_ON), |
| 95 | + KeyboardShortcutInfo("Forward", KeyEvent.KEYCODE_F, KeyEvent.META_CTRL_ON), |
| 96 | + KeyboardShortcutInfo("Backward", KeyEvent.KEYCODE_B, KeyEvent.META_CTRL_ON), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + val messageEdit = KeyboardShortcutGroup( |
| 101 | + "Message editing", |
| 102 | + listOf( |
| 103 | + KeyboardShortcutInfo("Select All", KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON), |
| 104 | + KeyboardShortcutInfo( |
| 105 | + "Send a message", |
| 106 | + KeyEvent.KEYCODE_ENTER, |
| 107 | + KeyEvent.META_SHIFT_ON |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + data?.add(cursorMovement) |
| 113 | + data?.add(messageEdit) |
| 114 | + } |
| 115 | + // [END android_compose_keyboard_shortcuts_helper_with_groups] |
| 116 | +} |
0 commit comments