blob: 93272139d42aebc6967417d6944133e01fddb701 [file] [log] [blame]
[email protected]0996e9b2011-08-26 17:59:011// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]3641da6c2009-07-08 14:59:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
andresantoso2389f842015-03-31 23:26:365#import <AppKit/AppKit.h>
[email protected]b6b0ca42009-09-18 16:24:246#include <Carbon/Carbon.h>
7
[email protected]3641da6c2009-07-08 14:59:068#include "chrome/browser/global_keyboard_shortcuts_mac.h"
9
10#include "base/basictypes.h"
[email protected]92f5adfa2010-01-05 09:49:1211#include "base/logging.h"
[email protected]1a3aba82010-11-08 23:52:5412#include "chrome/app/chrome_command_ids.h"
andresantoso2389f842015-03-31 23:26:3613#import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
14
15namespace {
16
17// Returns the menu item associated with |key| in |menu|, or nil if not found.
18NSMenuItem* FindMenuItem(NSEvent* key, NSMenu* menu) {
19 NSMenuItem* result = nil;
20
21 for (NSMenuItem* item in [menu itemArray]) {
22 NSMenu* submenu = [item submenu];
23 if (submenu) {
24 if (submenu != [NSApp servicesMenu])
25 result = FindMenuItem(key, submenu);
26 } else if ([item cr_firesForKeyEventIfEnabled:key]) {
27 result = item;
28 }
29
30 if (result)
31 break;
32 }
33
34 return result;
35}
36
37} // namespace
[email protected]3641da6c2009-07-08 14:59:0638
[email protected]1ade7b62009-12-18 08:56:1839// Basically, there are two kinds of keyboard shortcuts: Ones that should work
40// only if the tab contents is focused (BrowserKeyboardShortcut), and ones that
41// should work in all other cases (WindowKeyboardShortcut). In the latter case,
42// we differentiate between shortcuts that are checked before any other view
43// gets the chance to handle them (WindowKeyboardShortcut) or after all views
44// had a chance but did not handle the keypress event
[email protected]8656ff1f2010-11-19 22:18:1245// (DelayedWindowKeyboardShortcut).
[email protected]1ade7b62009-12-18 08:56:1846
[email protected]8656ff1f2010-11-19 22:18:1247const KeyboardShortcutData* GetWindowKeyboardShortcutTable(
48 size_t* num_entries) {
[email protected]3641da6c2009-07-08 14:59:0649 static const KeyboardShortcutData keyboard_shortcuts[] = {
[email protected]8656ff1f2010-11-19 22:18:1250 //cmd shift cntrl option
51 //--- ----- ----- ------
[email protected]92f5adfa2010-01-05 09:49:1252 // '{' / '}' characters should be matched earlier than virtual key code
53 // (therefore we can match alt-8 as '{' on german keyboards).
54 {true, false, false, false, 0, '}', IDC_SELECT_NEXT_TAB},
55 {true, false, false, false, 0, '{', IDC_SELECT_PREVIOUS_TAB},
56 {false, false, true, false, kVK_PageDown, 0, IDC_SELECT_NEXT_TAB},
57 {false, false, true, false, kVK_Tab, 0, IDC_SELECT_NEXT_TAB},
58 {false, false, true, false, kVK_PageUp, 0, IDC_SELECT_PREVIOUS_TAB},
59 {false, true, true, false, kVK_Tab, 0, IDC_SELECT_PREVIOUS_TAB},
[email protected]b6b0ca42009-09-18 16:24:2460 // Cmd-0..8 select the Nth tab, with cmd-9 being "last tab".
[email protected]b7b0bcb2010-11-17 17:12:2461 {true, false, false, false, kVK_ANSI_1, 0, IDC_SELECT_TAB_0},
62 {true, false, false, false, kVK_ANSI_Keypad1, 0, IDC_SELECT_TAB_0},
63 {true, false, false, false, kVK_ANSI_2, 0, IDC_SELECT_TAB_1},
64 {true, false, false, false, kVK_ANSI_Keypad2, 0, IDC_SELECT_TAB_1},
65 {true, false, false, false, kVK_ANSI_3, 0, IDC_SELECT_TAB_2},
66 {true, false, false, false, kVK_ANSI_Keypad3, 0, IDC_SELECT_TAB_2},
67 {true, false, false, false, kVK_ANSI_4, 0, IDC_SELECT_TAB_3},
68 {true, false, false, false, kVK_ANSI_Keypad4, 0, IDC_SELECT_TAB_3},
69 {true, false, false, false, kVK_ANSI_5, 0, IDC_SELECT_TAB_4},
70 {true, false, false, false, kVK_ANSI_Keypad5, 0, IDC_SELECT_TAB_4},
71 {true, false, false, false, kVK_ANSI_6, 0, IDC_SELECT_TAB_5},
72 {true, false, false, false, kVK_ANSI_Keypad6, 0, IDC_SELECT_TAB_5},
73 {true, false, false, false, kVK_ANSI_7, 0, IDC_SELECT_TAB_6},
74 {true, false, false, false, kVK_ANSI_Keypad7, 0, IDC_SELECT_TAB_6},
75 {true, false, false, false, kVK_ANSI_8, 0, IDC_SELECT_TAB_7},
76 {true, false, false, false, kVK_ANSI_Keypad8, 0, IDC_SELECT_TAB_7},
77 {true, false, false, false, kVK_ANSI_9, 0, IDC_SELECT_LAST_TAB},
78 {true, false, false, false, kVK_ANSI_Keypad9, 0, IDC_SELECT_LAST_TAB},
[email protected]413d24d2011-11-28 16:16:0579 {true, true, false, false, kVK_ANSI_M, 0, IDC_SHOW_AVATAR_MENU},
[email protected]3641da6c2009-07-08 14:59:0680 };
[email protected]70be00a2009-07-08 23:40:0881
[email protected]3641da6c2009-07-08 14:59:0682 *num_entries = arraysize(keyboard_shortcuts);
[email protected]70be00a2009-07-08 23:40:0883
[email protected]3641da6c2009-07-08 14:59:0684 return keyboard_shortcuts;
85}
86
[email protected]8656ff1f2010-11-19 22:18:1287const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable(
88 size_t* num_entries) {
[email protected]1ade7b62009-12-18 08:56:1889 static const KeyboardShortcutData keyboard_shortcuts[] = {
[email protected]8656ff1f2010-11-19 22:18:1290 //cmd shift cntrl option
91 //--- ----- ----- ------
[email protected]92f5adfa2010-01-05 09:49:1292 {false, false, false, false, kVK_Escape, 0, IDC_STOP},
[email protected]1ade7b62009-12-18 08:56:1893 };
94
95 *num_entries = arraysize(keyboard_shortcuts);
96
97 return keyboard_shortcuts;
98}
99
[email protected]8656ff1f2010-11-19 22:18:12100const KeyboardShortcutData* GetBrowserKeyboardShortcutTable(
101 size_t* num_entries) {
[email protected]1d313b832009-10-09 01:26:20102 static const KeyboardShortcutData keyboard_shortcuts[] = {
[email protected]8656ff1f2010-11-19 22:18:12103 //cmd shift cntrl option
104 //--- ----- ----- ------
[email protected]2a8a98122010-07-16 11:58:48105 {true, false, false, false, kVK_LeftArrow, 0, IDC_BACK},
106 {true, false, false, false, kVK_RightArrow, 0, IDC_FORWARD},
107 {false, false, false, false, kVK_Delete, 0, IDC_BACK},
108 {false, true, false, false, kVK_Delete, 0, IDC_FORWARD},
109 {true, true, false, false, 0, 'c', IDC_DEV_TOOLS_INSPECT},
[email protected]9207c592011-10-03 20:03:12110 {true, true, false, false, kVK_ANSI_Period, 0,
111 IDC_TOGGLE_SPEECH_INPUT},
[email protected]1d313b832009-10-09 01:26:20112 };
113
114 *num_entries = arraysize(keyboard_shortcuts);
115
116 return keyboard_shortcuts;
117}
118
[email protected]92f5adfa2010-01-05 09:49:12119static bool MatchesEventForKeyboardShortcut(
120 const KeyboardShortcutData& shortcut,
121 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
122 int vkey_code, unichar key_char) {
123 // Expects that one of |key_char| or |vkey_code| is 0.
124 DCHECK((shortcut.key_char == 0) ^ (shortcut.vkey_code == 0));
125 if (shortcut.key_char) {
126 // The given shortcut key is to be matched by a keyboard character.
127 // In this case we ignore shift and opt (alt) key modifiers, because
128 // the character may be generated by a combination with those keys.
129 if (shortcut.command_key == command_key &&
130 shortcut.cntrl_key == cntrl_key &&
131 shortcut.key_char == key_char)
132 return true;
133 } else if (shortcut.vkey_code) {
134 // The given shortcut key is to be matched by a virtual key code.
135 if (shortcut.command_key == command_key &&
136 shortcut.shift_key == shift_key &&
137 shortcut.cntrl_key == cntrl_key &&
138 shortcut.opt_key == opt_key &&
139 shortcut.vkey_code == vkey_code)
140 return true;
141 } else {
142 NOTREACHED(); // Shouldn't happen.
143 }
144 return false;
145}
146
[email protected]1d313b832009-10-09 01:26:20147static int CommandForKeyboardShortcut(
148 const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*),
[email protected]f7378a32009-10-21 17:15:28149 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
[email protected]92f5adfa2010-01-05 09:49:12150 int vkey_code, unichar key_char) {
[email protected]3641da6c2009-07-08 14:59:06151
152 // Scan through keycodes and see if it corresponds to one of the global
153 // shortcuts on file.
154 //
155 // TODO(jeremy): Change this into a hash table once we get enough
156 // entries in the array to make a difference.
[email protected]92f5adfa2010-01-05 09:49:12157 // (When turning this into a hash table, note that the current behavior
158 // relies on the order of the table (see the comment for '{' / '}' above).
[email protected]3641da6c2009-07-08 14:59:06159 size_t num_shortcuts = 0;
[email protected]1d313b832009-10-09 01:26:20160 const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts);
[email protected]3641da6c2009-07-08 14:59:06161 for (size_t i = 0; i < num_shortcuts; ++i, ++it) {
[email protected]92f5adfa2010-01-05 09:49:12162 if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key,
163 opt_key, vkey_code, key_char))
[email protected]3641da6c2009-07-08 14:59:06164 return it->chrome_command;
[email protected]3641da6c2009-07-08 14:59:06165 }
166
167 return -1;
168}
[email protected]1d313b832009-10-09 01:26:20169
170int CommandForWindowKeyboardShortcut(
[email protected]f7378a32009-10-21 17:15:28171 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
[email protected]92f5adfa2010-01-05 09:49:12172 int vkey_code, unichar key_char) {
[email protected]1d313b832009-10-09 01:26:20173 return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable,
174 command_key, shift_key,
[email protected]92f5adfa2010-01-05 09:49:12175 cntrl_key, opt_key, vkey_code,
176 key_char);
[email protected]1d313b832009-10-09 01:26:20177}
178
[email protected]1ade7b62009-12-18 08:56:18179int CommandForDelayedWindowKeyboardShortcut(
180 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
[email protected]92f5adfa2010-01-05 09:49:12181 int vkey_code, unichar key_char) {
[email protected]1ade7b62009-12-18 08:56:18182 return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable,
183 command_key, shift_key,
[email protected]92f5adfa2010-01-05 09:49:12184 cntrl_key, opt_key, vkey_code,
185 key_char);
[email protected]1ade7b62009-12-18 08:56:18186}
187
[email protected]1d313b832009-10-09 01:26:20188int CommandForBrowserKeyboardShortcut(
[email protected]f7378a32009-10-21 17:15:28189 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
[email protected]92f5adfa2010-01-05 09:49:12190 int vkey_code, unichar key_char) {
[email protected]1d313b832009-10-09 01:26:20191 return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable,
192 command_key, shift_key,
[email protected]92f5adfa2010-01-05 09:49:12193 cntrl_key, opt_key, vkey_code,
194 key_char);
195}
196
andresantoso2389f842015-03-31 23:26:36197int CommandForKeyEvent(NSEvent* event) {
198 if ([event type] != NSKeyDown)
199 return -1;
200
201 // Look in menu.
202 NSMenuItem* item = FindMenuItem(event, [NSApp mainMenu]);
203 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0)
204 return [item tag];
205
206 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items
207 // that do not correspond to IDC_ constants need no special treatment however,
208 // as they can't be blacklisted in
209 // |BrowserCommandController::IsReservedCommandOrKey()| anyhow.
210 if (item && [item action] == @selector(performClose:))
211 return IDC_CLOSE_WINDOW;
212
213 // "Exit" doesn't use the |commandDispatch:| mechanism either.
214 if (item && [item action] == @selector(terminate:))
215 return IDC_EXIT;
216
217 // Look in secondary keyboard shortcuts.
218 NSUInteger modifiers = [event modifierFlags];
219 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0;
220 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0;
221 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0;
222 const bool optKey = (modifiers & NSAlternateKeyMask) != 0;
223 const int keyCode = [event keyCode];
224 const unichar keyChar = KeyCharacterForEvent(event);
225
226 int cmdNum = CommandForWindowKeyboardShortcut(
227 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
228 if (cmdNum != -1)
229 return cmdNum;
230
231 cmdNum = CommandForBrowserKeyboardShortcut(
232 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
233 if (cmdNum != -1)
234 return cmdNum;
235
236 return -1;
237}
238
[email protected]92f5adfa2010-01-05 09:49:12239unichar KeyCharacterForEvent(NSEvent* event) {
[email protected]b881f4f42010-11-16 08:58:27240 NSString* eventString = [event charactersIgnoringModifiers];
241 NSString* characters = [event characters];
242
[email protected]92f5adfa2010-01-05 09:49:12243 if ([eventString length] != 1)
244 return 0;
245
246 if ([characters length] != 1)
247 return [eventString characterAtIndex:0];
248
[email protected]147f1242013-12-02 21:52:34249 // Some characters are BiDi mirrored. The mirroring is different
250 // for different OS versions. Instead of having a mirror table, map
251 // raw/processed pairs to desired outputs.
252 const struct {
253 unichar rawChar;
254 unichar unmodChar;
255 unichar targetChar;
256 } kCharMapping[] = {
257 // OSX 10.8 mirrors certain chars.
258 {'{', '}', '{'},
259 {'}', '{', '}'},
260 {'(', ')', '('},
261 {')', '(', ')'},
262
263 // OSX 10.9 has the unshifted raw char.
264 {'[', '}', '{'},
265 {']', '{', '}'},
266 {'9', ')', '('},
267 {'0', '(', ')'},
268
269 // These are the same either way.
270 {'[', ']', '['},
271 {']', '[', ']'},
272 };
273
[email protected]b881f4f42010-11-16 08:58:27274 unichar noModifiersChar = [eventString characterAtIndex:0];
275 unichar rawChar = [characters characterAtIndex:0];
[email protected]147f1242013-12-02 21:52:34276
277 // Only apply transformation table for ascii.
[email protected]b881f4f42010-11-16 08:58:27278 if (isascii(noModifiersChar) && isascii(rawChar)) {
[email protected]147f1242013-12-02 21:52:34279 // Alphabetic characters aren't mirrored, go with the raw character.
280 // [A previous partial comment said something about Dvorak?]
[email protected]b881f4f42010-11-16 08:58:27281 if (isalpha(rawChar))
282 return rawChar;
283
284 // http://crbug.com/42517
[email protected]147f1242013-12-02 21:52:34285 // http://crbug.com/315379
[email protected]b881f4f42010-11-16 08:58:27286 // In RTL keyboard layouts, Cocoa mirrors characters in the string
287 // returned by [event charactersIgnoringModifiers]. In this case, return
288 // the raw (unmirrored) char.
viettrungluu9e65ad12014-10-16 04:22:26289 for (size_t i = 0; i < arraysize(kCharMapping); ++i) {
[email protected]147f1242013-12-02 21:52:34290 if (rawChar == kCharMapping[i].rawChar &&
291 noModifiersChar == kCharMapping[i].unmodChar) {
292 return kCharMapping[i].targetChar;
293 }
[email protected]b881f4f42010-11-16 08:58:27294 }
295
[email protected]92f5adfa2010-01-05 09:49:12296 // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
297 if ([event modifierFlags] & NSAlternateKeyMask)
[email protected]147f1242013-12-02 21:52:34298 return rawChar;
[email protected]92f5adfa2010-01-05 09:49:12299 }
300
[email protected]147f1242013-12-02 21:52:34301 return noModifiersChar;
[email protected]1d313b832009-10-09 01:26:20302}