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