1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/accelerator_table.h"
6
7 #include "base/basictypes.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "ui/base/accelerators/accelerator.h"
10 #include "ui/events/event_constants.h"
11
12 #if defined(USE_ASH)
13 #include "ash/accelerators/accelerator_table.h"
14 #endif
15
16 namespace chrome {
17 namespace {
18
19 // NOTE: Keep this list in the same (mostly-alphabetical) order as
20 // the Windows accelerators in ../../app/chrome_dll.rc.
21 // Do not use Ctrl-Alt as a shortcut modifier, as it is used by i18n keyboards:
22 // http://blogs.msdn.com/b/oldnewthing/archive/2004/03/29/101121.aspx
23 const AcceleratorMapping kAcceleratorMap[] = {
24 { ui::VKEY_LEFT, ui::EF_ALT_DOWN, IDC_BACK },
25 { ui::VKEY_BACK, ui::EF_NONE, IDC_BACK },
26 { ui::VKEY_D, ui::EF_CONTROL_DOWN, IDC_BOOKMARK_PAGE },
27 { ui::VKEY_D, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
28 IDC_BOOKMARK_ALL_TABS },
29 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_TAB },
30 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
31 { ui::VKEY_F, ui::EF_CONTROL_DOWN, IDC_FIND },
32 { ui::VKEY_G, ui::EF_CONTROL_DOWN, IDC_FIND_NEXT },
33 { ui::VKEY_G, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_FIND_PREVIOUS },
34 { ui::VKEY_D, ui::EF_ALT_DOWN, IDC_FOCUS_LOCATION },
35 { ui::VKEY_L, ui::EF_CONTROL_DOWN, IDC_FOCUS_LOCATION },
36 { ui::VKEY_K, ui::EF_CONTROL_DOWN, IDC_FOCUS_SEARCH },
37 { ui::VKEY_E, ui::EF_CONTROL_DOWN, IDC_FOCUS_SEARCH },
38 { ui::VKEY_T, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN, IDC_FOCUS_TOOLBAR },
39 { ui::VKEY_B, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN, IDC_FOCUS_BOOKMARKS },
40 { ui::VKEY_A, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN, IDC_FOCUS_INFOBARS },
41 { ui::VKEY_RIGHT, ui::EF_ALT_DOWN, IDC_FORWARD },
42 { ui::VKEY_BACK, ui::EF_SHIFT_DOWN, IDC_FORWARD },
43 { ui::VKEY_I, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_DEV_TOOLS },
44 { ui::VKEY_F12, ui::EF_NONE, IDC_DEV_TOOLS_TOGGLE },
45 { ui::VKEY_J, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
46 IDC_DEV_TOOLS_CONSOLE },
47 { ui::VKEY_C, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
48 IDC_DEV_TOOLS_INSPECT },
49 { ui::VKEY_O, ui::EF_CONTROL_DOWN, IDC_OPEN_FILE },
50 { ui::VKEY_P, ui::EF_CONTROL_DOWN, IDC_PRINT},
51 #if !defined(DISABLE_BASIC_PRINTING)
52 { ui::VKEY_P, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_BASIC_PRINT},
53 #endif // !DISABLE_BASIC_PRINTING
54 { ui::VKEY_R, ui::EF_CONTROL_DOWN, IDC_RELOAD },
55 { ui::VKEY_R, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
56 IDC_RELOAD_IGNORING_CACHE },
57 { ui::VKEY_HOME, ui::EF_ALT_DOWN, IDC_HOME },
58 { ui::VKEY_S, ui::EF_CONTROL_DOWN, IDC_SAVE_PAGE },
59 { ui::VKEY_9, ui::EF_CONTROL_DOWN, IDC_SELECT_LAST_TAB },
60 { ui::VKEY_NUMPAD9, ui::EF_CONTROL_DOWN, IDC_SELECT_LAST_TAB },
61 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
62 { ui::VKEY_9, ui::EF_ALT_DOWN, IDC_SELECT_LAST_TAB },
63 { ui::VKEY_NUMPAD9, ui::EF_ALT_DOWN, IDC_SELECT_LAST_TAB },
64 { ui::VKEY_NEXT, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN, IDC_MOVE_TAB_NEXT },
65 { ui::VKEY_PRIOR, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN,
66 IDC_MOVE_TAB_PREVIOUS },
67 #endif
68 { ui::VKEY_TAB, ui::EF_CONTROL_DOWN, IDC_SELECT_NEXT_TAB },
69 { ui::VKEY_NEXT, ui::EF_CONTROL_DOWN, IDC_SELECT_NEXT_TAB },
70 { ui::VKEY_TAB, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
71 IDC_SELECT_PREVIOUS_TAB },
72 { ui::VKEY_PRIOR, ui::EF_CONTROL_DOWN, IDC_SELECT_PREVIOUS_TAB },
73 { ui::VKEY_1, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_0 },
74 { ui::VKEY_NUMPAD1, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_0 },
75 { ui::VKEY_2, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_1 },
76 { ui::VKEY_NUMPAD2, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_1 },
77 { ui::VKEY_3, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_2 },
78 { ui::VKEY_NUMPAD3, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_2 },
79 { ui::VKEY_4, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_3 },
80 { ui::VKEY_NUMPAD4, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_3 },
81 { ui::VKEY_5, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_4 },
82 { ui::VKEY_NUMPAD5, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_4 },
83 { ui::VKEY_6, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_5 },
84 { ui::VKEY_NUMPAD6, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_5 },
85 { ui::VKEY_7, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_6 },
86 { ui::VKEY_NUMPAD7, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_6 },
87 { ui::VKEY_8, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_7 },
88 { ui::VKEY_NUMPAD8, ui::EF_CONTROL_DOWN, IDC_SELECT_TAB_7 },
89 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
90 { ui::VKEY_1, ui::EF_ALT_DOWN, IDC_SELECT_TAB_0 },
91 { ui::VKEY_NUMPAD1, ui::EF_ALT_DOWN, IDC_SELECT_TAB_0 },
92 { ui::VKEY_2, ui::EF_ALT_DOWN, IDC_SELECT_TAB_1 },
93 { ui::VKEY_NUMPAD2, ui::EF_ALT_DOWN, IDC_SELECT_TAB_1 },
94 { ui::VKEY_3, ui::EF_ALT_DOWN, IDC_SELECT_TAB_2 },
95 { ui::VKEY_NUMPAD3, ui::EF_ALT_DOWN, IDC_SELECT_TAB_2 },
96 { ui::VKEY_4, ui::EF_ALT_DOWN, IDC_SELECT_TAB_3 },
97 { ui::VKEY_NUMPAD4, ui::EF_ALT_DOWN, IDC_SELECT_TAB_3 },
98 { ui::VKEY_5, ui::EF_ALT_DOWN, IDC_SELECT_TAB_4 },
99 { ui::VKEY_NUMPAD5, ui::EF_ALT_DOWN, IDC_SELECT_TAB_4 },
100 { ui::VKEY_6, ui::EF_ALT_DOWN, IDC_SELECT_TAB_5 },
101 { ui::VKEY_NUMPAD6, ui::EF_ALT_DOWN, IDC_SELECT_TAB_5 },
102 { ui::VKEY_7, ui::EF_ALT_DOWN, IDC_SELECT_TAB_6 },
103 { ui::VKEY_NUMPAD7, ui::EF_ALT_DOWN, IDC_SELECT_TAB_6 },
104 { ui::VKEY_8, ui::EF_ALT_DOWN, IDC_SELECT_TAB_7 },
105 { ui::VKEY_NUMPAD8, ui::EF_ALT_DOWN, IDC_SELECT_TAB_7 },
106 { ui::VKEY_BROWSER_FAVORITES, ui::EF_NONE, IDC_SHOW_BOOKMARK_BAR },
107 #endif
108 { ui::VKEY_B, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
109 IDC_SHOW_BOOKMARK_BAR },
110 { ui::VKEY_O, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
111 IDC_SHOW_BOOKMARK_MANAGER },
112 { ui::VKEY_J, ui::EF_CONTROL_DOWN, IDC_SHOW_DOWNLOADS },
113 { ui::VKEY_H, ui::EF_CONTROL_DOWN, IDC_SHOW_HISTORY },
114 { ui::VKEY_F, ui::EF_ALT_DOWN, IDC_SHOW_APP_MENU},
115 { ui::VKEY_E, ui::EF_ALT_DOWN, IDC_SHOW_APP_MENU},
116 { ui::VKEY_ESCAPE, ui::EF_NONE, IDC_STOP },
117 { ui::VKEY_OEM_PERIOD, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
118 IDC_TOGGLE_SPEECH_INPUT },
119 { ui::VKEY_U, ui::EF_CONTROL_DOWN, IDC_VIEW_SOURCE },
120 { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
121 { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
122 { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
123 { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
124 { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
125 { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
126 { ui::VKEY_F1, ui::EF_NONE, IDC_HELP_PAGE_VIA_KEYBOARD },
127 { ui::VKEY_F3, ui::EF_NONE, IDC_FIND_NEXT },
128 { ui::VKEY_F3, ui::EF_SHIFT_DOWN, IDC_FIND_PREVIOUS },
129 { ui::VKEY_F4, ui::EF_CONTROL_DOWN, IDC_CLOSE_TAB },
130 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
131 { ui::VKEY_F5, ui::EF_NONE, IDC_RELOAD },
132 { ui::VKEY_F5, ui::EF_CONTROL_DOWN, IDC_RELOAD_IGNORING_CACHE },
133 { ui::VKEY_F5, ui::EF_SHIFT_DOWN, IDC_RELOAD_IGNORING_CACHE },
134 { ui::VKEY_F6, ui::EF_NONE, IDC_FOCUS_NEXT_PANE },
135 { ui::VKEY_F6, ui::EF_SHIFT_DOWN, IDC_FOCUS_PREVIOUS_PANE },
136 { ui::VKEY_F10, ui::EF_NONE, IDC_FOCUS_MENU_BAR },
137 { ui::VKEY_F11, ui::EF_NONE, IDC_FULLSCREEN },
138 { ui::VKEY_ESCAPE, ui::EF_SHIFT_DOWN, IDC_TASK_MANAGER },
139
140 // Platform-specific key maps.
141 #if defined(OS_LINUX)
142 { ui::VKEY_BROWSER_BACK, ui::EF_NONE, IDC_BACK },
143 { ui::VKEY_BROWSER_FORWARD, ui::EF_NONE, IDC_FORWARD },
144 { ui::VKEY_BROWSER_HOME, ui::EF_NONE, IDC_HOME },
145 { ui::VKEY_BROWSER_REFRESH, ui::EF_NONE, IDC_RELOAD },
146 { ui::VKEY_BROWSER_REFRESH, ui::EF_CONTROL_DOWN, IDC_RELOAD_IGNORING_CACHE },
147 { ui::VKEY_BROWSER_REFRESH, ui::EF_SHIFT_DOWN, IDC_RELOAD_IGNORING_CACHE },
148 #endif // defined(OS_LINUX)
149
150 #if defined(OS_CHROMEOS)
151 // On Chrome OS, VKEY_BROWSER_SEARCH is handled in Ash.
152 { ui::VKEY_BACK, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
153 IDC_CLEAR_BROWSING_DATA },
154 { ui::VKEY_OEM_2, ui::EF_CONTROL_DOWN, IDC_HELP_PAGE_VIA_KEYBOARD },
155 { ui::VKEY_OEM_2, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
156 IDC_HELP_PAGE_VIA_KEYBOARD },
157 { ui::VKEY_BROWSER_FAVORITES, ui::EF_NONE, IDC_SHOW_BOOKMARK_MANAGER },
158 { ui::VKEY_BROWSER_STOP, ui::EF_NONE, IDC_STOP },
159 #else // OS_CHROMEOS
160 { ui::VKEY_DELETE, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
161 IDC_CLEAR_BROWSING_DATA },
162 { ui::VKEY_LMENU, ui::EF_NONE, IDC_FOCUS_MENU_BAR },
163 { ui::VKEY_MENU, ui::EF_NONE, IDC_FOCUS_MENU_BAR },
164 { ui::VKEY_RMENU, ui::EF_NONE, IDC_FOCUS_MENU_BAR },
165 // On Windows, all VKEY_BROWSER_* keys except VKEY_BROWSER_SEARCH are handled
166 // via WM_APPCOMMAND.
167 { ui::VKEY_BROWSER_SEARCH, ui::EF_NONE, IDC_FOCUS_SEARCH },
168 { ui::VKEY_M, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_SHOW_AVATAR_MENU},
169 // On ChromeOS, these keys are assigned to change UI scale.
170 { ui::VKEY_OEM_PLUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
171 { ui::VKEY_OEM_MINUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
172 IDC_ZOOM_MINUS },
173 // For each entry here add an entry into kChromeCmdId2AshActionId below
174 // if Ash has a corresponding accelerator.
175 #if defined(GOOGLE_CHROME_BUILD)
176 { ui::VKEY_I, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN, IDC_FEEDBACK },
177 #endif
178 { ui::VKEY_Q, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_EXIT },
179 { ui::VKEY_N, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
180 IDC_NEW_INCOGNITO_WINDOW },
181 { ui::VKEY_T, ui::EF_CONTROL_DOWN, IDC_NEW_TAB },
182 { ui::VKEY_N, ui::EF_CONTROL_DOWN, IDC_NEW_WINDOW },
183 { ui::VKEY_T, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_RESTORE_TAB },
184 #endif
185 };
186 const size_t kAcceleratorMapLength = arraysize(kAcceleratorMap);
187
188 #if defined(USE_ASH)
189 // Below we map Chrome command ids to Ash action ids for commands that have
190 // an shortcut that is handled by Ash (instead of Chrome). Adding entries
191 // here will show shortcut text on menus. See comment above.
192 struct ChromeCmdId2AshActionId {
193 const int chrome_cmd_id;
194 const ash::AcceleratorAction ash_action_id;
195 };
196 const ChromeCmdId2AshActionId kChromeCmdId2AshActionId[] = {
197 #if defined(GOOGLE_CHROME_BUILD)
198 { IDC_FEEDBACK, ash::OPEN_FEEDBACK_PAGE },
199 #endif
200 { IDC_EXIT, ash::EXIT },
201 { IDC_NEW_INCOGNITO_WINDOW, ash::NEW_INCOGNITO_WINDOW },
202 { IDC_NEW_TAB, ash::NEW_TAB },
203 { IDC_NEW_WINDOW, ash::NEW_WINDOW },
204 { IDC_RESTORE_TAB, ash::RESTORE_TAB },
205 { IDC_TASK_MANAGER, ash::SHOW_TASK_MANAGER },
206 };
207 const size_t kChromeCmdId2AshActionIdLength =
208 arraysize(kChromeCmdId2AshActionId);
209 #endif // defined(USE_ASH)
210
211 } // namespace
212
GetAcceleratorList()213 std::vector<AcceleratorMapping> GetAcceleratorList() {
214 return std::vector<AcceleratorMapping>(
215 kAcceleratorMap, kAcceleratorMap + kAcceleratorMapLength);
216 }
217
GetAshAcceleratorForCommandId(int command_id,HostDesktopType host_desktop_type,ui::Accelerator * accelerator)218 bool GetAshAcceleratorForCommandId(int command_id,
219 HostDesktopType host_desktop_type,
220 ui::Accelerator* accelerator) {
221 #if defined(USE_ASH)
222 if (host_desktop_type != chrome::HOST_DESKTOP_TYPE_ASH)
223 return false;
224 for (size_t i = 0; i < kChromeCmdId2AshActionIdLength; ++i) {
225 if (command_id == kChromeCmdId2AshActionId[i].chrome_cmd_id) {
226 for (size_t j = 0; j < ash::kAcceleratorDataLength; ++j) {
227 if (kChromeCmdId2AshActionId[i].ash_action_id ==
228 ash::kAcceleratorData[j].action) {
229 *accelerator = ui::Accelerator(ash::kAcceleratorData[j].keycode,
230 ash::kAcceleratorData[j].modifiers);
231 return true;
232 }
233 }
234 }
235 }
236 #endif // defined(USE_ASH)
237 return false;
238 }
239
GetStandardAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator)240 bool GetStandardAcceleratorForCommandId(int command_id,
241 ui::Accelerator* accelerator) {
242 // The standard Ctrl-X, Ctrl-V and Ctrl-C are not defined as accelerators
243 // anywhere else.
244 switch (command_id) {
245 case IDC_CUT:
246 *accelerator = ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN);
247 return true;
248 case IDC_COPY:
249 *accelerator = ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN);
250 return true;
251 case IDC_PASTE:
252 *accelerator = ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN);
253 return true;
254 }
255 return false;
256 }
257
258 } // namespace chrome
259