OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import android.view.KeyEvent; | 7 import android.view.KeyEvent; |
8 | 8 |
9 import org.chromium.base.annotations.SuppressFBWarnings; | 9 import org.chromium.base.annotations.SuppressFBWarnings; |
10 import org.chromium.chrome.R; | 10 import org.chromium.chrome.R; |
(...skipping 16 matching lines...) Expand all Loading... |
27 private static final int SHIFT = 1 << 29; | 27 private static final int SHIFT = 1 << 29; |
28 | 28 |
29 private KeyboardShortcuts() {} | 29 private KeyboardShortcuts() {} |
30 | 30 |
31 private static int getMetaState(KeyEvent event) { | 31 private static int getMetaState(KeyEvent event) { |
32 return (event.isCtrlPressed() ? CTRL : 0) | 32 return (event.isCtrlPressed() ? CTRL : 0) |
33 | (event.isAltPressed() ? ALT : 0) | 33 | (event.isAltPressed() ? ALT : 0) |
34 | (event.isShiftPressed() ? SHIFT : 0); | 34 | (event.isShiftPressed() ? SHIFT : 0); |
35 } | 35 } |
36 | 36 |
| 37 private static boolean isGamepadAPIActive(ChromeActivity activity) { |
| 38 ContentViewCore cvc = activity.getCurrentContentViewCore(); |
| 39 return (cvc != null) ? cvc.isGamepadAPIActive() : false; |
| 40 } |
| 41 |
37 /** | 42 /** |
38 * This should be called from the Activity's dispatchKeyEvent() to handle ke
yboard shortcuts. | 43 * This should be called from the Activity's dispatchKeyEvent() to handle ke
yboard shortcuts. |
39 * | 44 * |
40 * Note: dispatchKeyEvent() is called before the active view or web page get
s a chance to handle | 45 * Note: dispatchKeyEvent() is called before the active view or web page get
s a chance to handle |
41 * the key event. So the keys handled here cannot be overridden by any view
or web page. | 46 * the key event. So the keys handled here cannot be overridden by any view
or web page. |
42 * | 47 * |
43 * @param event The KeyEvent to handle. | 48 * @param event The KeyEvent to handle. |
44 * @param activity The ChromeActivity in which the key was pressed. | 49 * @param activity The ChromeActivity in which the key was pressed. |
45 * @param uiInitialized Whether the UI has been initialized. If this is fals
e, most keys will | 50 * @param uiInitialized Whether the UI has been initialized. If this is fals
e, most keys will |
46 * not be handled. | 51 * not be handled. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 * @param isCurrentTabVisible Whether page-related actions are valid, e.g. r
eload, zoom in. | 103 * @param isCurrentTabVisible Whether page-related actions are valid, e.g. r
eload, zoom in. |
99 * This should be false when in the tab switcher. | 104 * This should be false when in the tab switcher. |
100 * @param tabSwitchingEnabled Whether shortcuts that switch between tabs are
enabled (e.g. | 105 * @param tabSwitchingEnabled Whether shortcuts that switch between tabs are
enabled (e.g. |
101 * Ctrl+Tab, Ctrl+3). | 106 * Ctrl+Tab, Ctrl+3). |
102 * @return Whether the key event was handled. | 107 * @return Whether the key event was handled. |
103 */ | 108 */ |
104 public static boolean onKeyDown(KeyEvent event, ChromeActivity activity, | 109 public static boolean onKeyDown(KeyEvent event, ChromeActivity activity, |
105 boolean isCurrentTabVisible, boolean tabSwitchingEnabled) { | 110 boolean isCurrentTabVisible, boolean tabSwitchingEnabled) { |
106 int keyCode = event.getKeyCode(); | 111 int keyCode = event.getKeyCode(); |
107 if (event.getRepeatCount() != 0 || KeyEvent.isModifierKey(keyCode)) retu
rn false; | 112 if (event.getRepeatCount() != 0 || KeyEvent.isModifierKey(keyCode)) retu
rn false; |
108 if (!event.isCtrlPressed() && !event.isAltPressed() | 113 if (KeyEvent.isGamepadButton(keyCode)) { |
| 114 if (isGamepadAPIActive(activity)) return false; |
| 115 } else if (!event.isCtrlPressed() && !event.isAltPressed() |
109 && keyCode != KeyEvent.KEYCODE_F3 | 116 && keyCode != KeyEvent.KEYCODE_F3 |
110 && keyCode != KeyEvent.KEYCODE_F5 | 117 && keyCode != KeyEvent.KEYCODE_F5 |
111 && keyCode != KeyEvent.KEYCODE_FORWARD) { | 118 && keyCode != KeyEvent.KEYCODE_FORWARD) { |
112 return false; | 119 return false; |
113 } | 120 } |
114 | 121 |
115 TabModel curModel = activity.getCurrentTabModel(); | 122 TabModel curModel = activity.getCurrentTabModel(); |
116 int count = curModel.getCount(); | 123 int count = curModel.getCount(); |
117 | 124 |
118 int metaState = getMetaState(event); | 125 int metaState = getMetaState(event); |
(...skipping 20 matching lines...) Expand all Loading... |
139 if (currentTab != null && isCurrentTabVisible) { | 146 if (currentTab != null && isCurrentTabVisible) { |
140 currentTab.loadUrl(new LoadUrlParams(url, PageTransition.AUT
O_BOOKMARK)); | 147 currentTab.loadUrl(new LoadUrlParams(url, PageTransition.AUT
O_BOOKMARK)); |
141 } else { | 148 } else { |
142 TabCreator tabCreator = activity.getCurrentTabCreator(); | 149 TabCreator tabCreator = activity.getCurrentTabCreator(); |
143 if (tabCreator != null) { | 150 if (tabCreator != null) { |
144 tabCreator.launchUrl(url, TabLaunchType.FROM_KEYBOARD); | 151 tabCreator.launchUrl(url, TabLaunchType.FROM_KEYBOARD); |
145 } | 152 } |
146 } | 153 } |
147 return true; | 154 return true; |
148 case ALT | KeyEvent.KEYCODE_F: | 155 case ALT | KeyEvent.KEYCODE_F: |
| 156 case KeyEvent.KEYCODE_BUTTON_Y: |
149 activity.onMenuOrKeyboardAction(R.id.show_menu, false); | 157 activity.onMenuOrKeyboardAction(R.id.show_menu, false); |
150 return true; | 158 return true; |
151 } | 159 } |
152 | 160 |
153 if (isCurrentTabVisible) { | 161 if (isCurrentTabVisible) { |
154 if (tabSwitchingEnabled && (metaState == CTRL || metaState == ALT))
{ | 162 if (tabSwitchingEnabled && (metaState == CTRL || metaState == ALT))
{ |
155 int numCode = keyCode - KeyEvent.KEYCODE_0; | 163 int numCode = keyCode - KeyEvent.KEYCODE_0; |
156 if (numCode > 0 && numCode <= Math.min(count, 8)) { | 164 if (numCode > 0 && numCode <= Math.min(count, 8)) { |
157 // Ctrl+1 to Ctrl+8: select tab by index | 165 // Ctrl+1 to Ctrl+8: select tab by index |
158 TabModelUtils.setIndex(curModel, numCode - 1); | 166 TabModelUtils.setIndex(curModel, numCode - 1); |
159 return true; | 167 return true; |
160 } else if (numCode == 9 && count != 0) { | 168 } else if (numCode == 9 && count != 0) { |
161 // Ctrl+9: select last tab | 169 // Ctrl+9: select last tab |
162 TabModelUtils.setIndex(curModel, count - 1); | 170 TabModelUtils.setIndex(curModel, count - 1); |
163 return true; | 171 return true; |
164 } | 172 } |
165 } | 173 } |
166 | 174 |
167 switch (keyCodeAndMeta) { | 175 switch (keyCodeAndMeta) { |
168 case CTRL | KeyEvent.KEYCODE_TAB: | 176 case CTRL | KeyEvent.KEYCODE_TAB: |
169 case CTRL | KeyEvent.KEYCODE_PAGE_DOWN: | 177 case CTRL | KeyEvent.KEYCODE_PAGE_DOWN: |
| 178 case KeyEvent.KEYCODE_BUTTON_R1: |
170 if (tabSwitchingEnabled && count > 1) { | 179 if (tabSwitchingEnabled && count > 1) { |
171 TabModelUtils.setIndex(curModel, (curModel.index() + 1)
% count); | 180 TabModelUtils.setIndex(curModel, (curModel.index() + 1)
% count); |
172 } | 181 } |
173 return true; | 182 return true; |
174 case CTRL | SHIFT | KeyEvent.KEYCODE_TAB: | 183 case CTRL | SHIFT | KeyEvent.KEYCODE_TAB: |
175 case CTRL | KeyEvent.KEYCODE_PAGE_UP: | 184 case CTRL | KeyEvent.KEYCODE_PAGE_UP: |
| 185 case KeyEvent.KEYCODE_BUTTON_L1: |
176 if (tabSwitchingEnabled && count > 1) { | 186 if (tabSwitchingEnabled && count > 1) { |
177 TabModelUtils.setIndex(curModel, (curModel.index() + cou
nt - 1) % count); | 187 TabModelUtils.setIndex(curModel, (curModel.index() + cou
nt - 1) % count); |
178 } | 188 } |
179 return true; | 189 return true; |
180 case CTRL | KeyEvent.KEYCODE_W: | 190 case CTRL | KeyEvent.KEYCODE_W: |
181 case CTRL | KeyEvent.KEYCODE_F4: | 191 case CTRL | KeyEvent.KEYCODE_F4: |
| 192 case KeyEvent.KEYCODE_BUTTON_B: |
182 TabModelUtils.closeCurrentTab(curModel); | 193 TabModelUtils.closeCurrentTab(curModel); |
183 return true; | 194 return true; |
184 case CTRL | KeyEvent.KEYCODE_F: | 195 case CTRL | KeyEvent.KEYCODE_F: |
185 case CTRL | KeyEvent.KEYCODE_G: | 196 case CTRL | KeyEvent.KEYCODE_G: |
186 case CTRL | SHIFT | KeyEvent.KEYCODE_G: | 197 case CTRL | SHIFT | KeyEvent.KEYCODE_G: |
187 case KeyEvent.KEYCODE_F3: | 198 case KeyEvent.KEYCODE_F3: |
188 case SHIFT | KeyEvent.KEYCODE_F3: | 199 case SHIFT | KeyEvent.KEYCODE_F3: |
189 activity.onMenuOrKeyboardAction(R.id.find_in_page_id, false)
; | 200 activity.onMenuOrKeyboardAction(R.id.find_in_page_id, false)
; |
190 return true; | 201 return true; |
191 case CTRL | KeyEvent.KEYCODE_L: | 202 case CTRL | KeyEvent.KEYCODE_L: |
192 case ALT | KeyEvent.KEYCODE_D: | 203 case ALT | KeyEvent.KEYCODE_D: |
| 204 case KeyEvent.KEYCODE_BUTTON_X: |
193 activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false); | 205 activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false); |
194 return true; | 206 return true; |
195 case KeyEvent.KEYCODE_BOOKMARK: | 207 case KeyEvent.KEYCODE_BOOKMARK: |
196 case CTRL | KeyEvent.KEYCODE_D: | 208 case CTRL | KeyEvent.KEYCODE_D: |
197 activity.onMenuOrKeyboardAction(R.id.bookmark_this_page_id,
false); | 209 activity.onMenuOrKeyboardAction(R.id.bookmark_this_page_id,
false); |
198 return true; | 210 return true; |
199 case CTRL | KeyEvent.KEYCODE_P: | 211 case CTRL | KeyEvent.KEYCODE_P: |
200 activity.onMenuOrKeyboardAction(R.id.print_id, false); | 212 activity.onMenuOrKeyboardAction(R.id.print_id, false); |
201 return true; | 213 return true; |
202 case CTRL | KeyEvent.KEYCODE_PLUS: | 214 case CTRL | KeyEvent.KEYCODE_PLUS: |
(...skipping 17 matching lines...) Expand all Loading... |
220 case KeyEvent.KEYCODE_F5: | 232 case KeyEvent.KEYCODE_F5: |
221 Tab tab = activity.getActivityTab(); | 233 Tab tab = activity.getActivityTab(); |
222 if (tab != null) tab.reload(); | 234 if (tab != null) tab.reload(); |
223 return true; | 235 return true; |
224 case ALT | KeyEvent.KEYCODE_DPAD_LEFT: | 236 case ALT | KeyEvent.KEYCODE_DPAD_LEFT: |
225 tab = activity.getActivityTab(); | 237 tab = activity.getActivityTab(); |
226 if (tab != null && tab.canGoBack()) tab.goBack(); | 238 if (tab != null && tab.canGoBack()) tab.goBack(); |
227 return true; | 239 return true; |
228 case ALT | KeyEvent.KEYCODE_DPAD_RIGHT: | 240 case ALT | KeyEvent.KEYCODE_DPAD_RIGHT: |
229 case KeyEvent.KEYCODE_FORWARD: | 241 case KeyEvent.KEYCODE_FORWARD: |
| 242 case KeyEvent.KEYCODE_BUTTON_START: |
230 tab = activity.getActivityTab(); | 243 tab = activity.getActivityTab(); |
231 if (tab != null && tab.canGoForward()) tab.goForward(); | 244 if (tab != null && tab.canGoForward()) tab.goForward(); |
232 return true; | 245 return true; |
233 case CTRL | SHIFT | KeyEvent.KEYCODE_SLASH: // i.e. Ctrl+? | 246 case CTRL | SHIFT | KeyEvent.KEYCODE_SLASH: // i.e. Ctrl+? |
234 activity.onMenuOrKeyboardAction(R.id.help_id, false); | 247 activity.onMenuOrKeyboardAction(R.id.help_id, false); |
235 return true; | 248 return true; |
236 } | 249 } |
237 } | 250 } |
238 | 251 |
239 return false; | 252 return false; |
240 } | 253 } |
241 } | 254 } |
OLD | NEW |