1 // Copyright (c) 2011 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.
5 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
7 #import <AppKit/AppKit.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
16 // Returns the menu item associated with |key| in |menu|, or nil if not found.
17 NSMenuItem* FindMenuItem(NSEvent* key, NSMenu* menu) {
18 NSMenuItem* result = nil;
20 for (NSMenuItem* item in [menu itemArray]) {
21 NSMenu* submenu = [item submenu];
23 if (submenu != [NSApp servicesMenu])
24 result = FindMenuItem(key, submenu);
25 } else if ([item cr_firesForKeyEventIfEnabled:key]) {
38 // Basically, there are two kinds of keyboard shortcuts: Ones that should work
39 // only if the tab contents is focused (BrowserKeyboardShortcut), and ones that
40 // should work in all other cases (WindowKeyboardShortcut). In the latter case,
41 // we differentiate between shortcuts that are checked before any other view
42 // gets the chance to handle them (WindowKeyboardShortcut) or after all views
43 // had a chance but did not handle the keypress event
44 // (DelayedWindowKeyboardShortcut).
46 const KeyboardShortcutData* GetWindowKeyboardShortcutTable(
47 size_t* num_entries) {
48 static const KeyboardShortcutData keyboard_shortcuts[] = {
49 //cmd shift cntrl option
50 //--- ----- ----- ------
51 // '{' / '}' characters should be matched earlier than virtual key code
52 // (therefore we can match alt-8 as '{' on german keyboards).
53 {true, false, false, false, 0, '}', IDC_SELECT_NEXT_TAB},
54 {true, false, false, false, 0, '{', IDC_SELECT_PREVIOUS_TAB},
55 {false, false, true, false, kVK_PageDown, 0, IDC_SELECT_NEXT_TAB},
56 {false, false, true, false, kVK_Tab, 0, IDC_SELECT_NEXT_TAB},
57 {false, false, true, false, kVK_PageUp, 0, IDC_SELECT_PREVIOUS_TAB},
58 {false, true, true, false, kVK_Tab, 0, IDC_SELECT_PREVIOUS_TAB},
59 // Cmd-0..8 select the Nth tab, with cmd-9 being "last tab".
60 {true, false, false, false, kVK_ANSI_1, 0, IDC_SELECT_TAB_0},
61 {true, false, false, false, kVK_ANSI_Keypad1, 0, IDC_SELECT_TAB_0},
62 {true, false, false, false, kVK_ANSI_2, 0, IDC_SELECT_TAB_1},
63 {true, false, false, false, kVK_ANSI_Keypad2, 0, IDC_SELECT_TAB_1},
64 {true, false, false, false, kVK_ANSI_3, 0, IDC_SELECT_TAB_2},
65 {true, false, false, false, kVK_ANSI_Keypad3, 0, IDC_SELECT_TAB_2},
66 {true, false, false, false, kVK_ANSI_4, 0, IDC_SELECT_TAB_3},
67 {true, false, false, false, kVK_ANSI_Keypad4, 0, IDC_SELECT_TAB_3},
68 {true, false, false, false, kVK_ANSI_5, 0, IDC_SELECT_TAB_4},
69 {true, false, false, false, kVK_ANSI_Keypad5, 0, IDC_SELECT_TAB_4},
70 {true, false, false, false, kVK_ANSI_6, 0, IDC_SELECT_TAB_5},
71 {true, false, false, false, kVK_ANSI_Keypad6, 0, IDC_SELECT_TAB_5},
72 {true, false, false, false, kVK_ANSI_7, 0, IDC_SELECT_TAB_6},
73 {true, false, false, false, kVK_ANSI_Keypad7, 0, IDC_SELECT_TAB_6},
74 {true, false, false, false, kVK_ANSI_8, 0, IDC_SELECT_TAB_7},
75 {true, false, false, false, kVK_ANSI_Keypad8, 0, IDC_SELECT_TAB_7},
76 {true, false, false, false, kVK_ANSI_9, 0, IDC_SELECT_LAST_TAB},
77 {true, false, false, false, kVK_ANSI_Keypad9, 0, IDC_SELECT_LAST_TAB},
78 {true, true, false, false, kVK_ANSI_M, 0, IDC_SHOW_AVATAR_MENU},
81 *num_entries = arraysize(keyboard_shortcuts);
83 return keyboard_shortcuts;
86 const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable(
87 size_t* num_entries) {
88 static const KeyboardShortcutData keyboard_shortcuts[] = {
89 //cmd shift cntrl option
90 //--- ----- ----- ------
91 {false, false, false, false, kVK_Escape, 0, IDC_STOP},
94 *num_entries = arraysize(keyboard_shortcuts);
96 return keyboard_shortcuts;
99 const KeyboardShortcutData* GetBrowserKeyboardShortcutTable(
100 size_t* num_entries) {
101 static const KeyboardShortcutData keyboard_shortcuts[] = {
102 //cmd shift cntrl option
103 //--- ----- ----- ------
104 {true, false, false, false, kVK_LeftArrow, 0, IDC_BACK},
105 {true, false, false, false, kVK_RightArrow, 0, IDC_FORWARD},
106 {false, false, false, false, kVK_Delete, 0, IDC_BACK},
107 {false, true, false, false, kVK_Delete, 0, IDC_FORWARD},
108 {true, true, false, false, 0, 'c', IDC_DEV_TOOLS_INSPECT},
109 {true, true, false, false, kVK_ANSI_Period, 0,
110 IDC_TOGGLE_SPEECH_INPUT},
113 *num_entries = arraysize(keyboard_shortcuts);
115 return keyboard_shortcuts;
118 static bool MatchesEventForKeyboardShortcut(
119 const KeyboardShortcutData& shortcut,
120 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
121 int vkey_code, unichar key_char) {
122 // Expects that one of |key_char| or |vkey_code| is 0.
123 DCHECK((shortcut.key_char == 0) ^ (shortcut.vkey_code == 0));
124 if (shortcut.key_char) {
125 // The given shortcut key is to be matched by a keyboard character.
126 // In this case we ignore shift and opt (alt) key modifiers, because
127 // the character may be generated by a combination with those keys.
128 if (shortcut.command_key == command_key &&
129 shortcut.cntrl_key == cntrl_key &&
130 shortcut.key_char == key_char)
132 } else if (shortcut.vkey_code) {
133 // The given shortcut key is to be matched by a virtual key code.
134 if (shortcut.command_key == command_key &&
135 shortcut.shift_key == shift_key &&
136 shortcut.cntrl_key == cntrl_key &&
137 shortcut.opt_key == opt_key &&
138 shortcut.vkey_code == vkey_code)
141 NOTREACHED(); // Shouldn't happen.
146 static int CommandForKeyboardShortcut(
147 const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*),
148 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
149 int vkey_code, unichar key_char) {
151 // Scan through keycodes and see if it corresponds to one of the global
152 // shortcuts on file.
154 // TODO(jeremy): Change this into a hash table once we get enough
155 // entries in the array to make a difference.
156 // (When turning this into a hash table, note that the current behavior
157 // relies on the order of the table (see the comment for '{' / '}' above).
158 size_t num_shortcuts = 0;
159 const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts);
160 for (size_t i = 0; i < num_shortcuts; ++i, ++it) {
161 if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key,
162 opt_key, vkey_code, key_char))
163 return it->chrome_command;
169 int CommandForWindowKeyboardShortcut(
170 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
171 int vkey_code, unichar key_char) {
172 return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable,
173 command_key, shift_key,
174 cntrl_key, opt_key, vkey_code,
178 int CommandForDelayedWindowKeyboardShortcut(
179 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
180 int vkey_code, unichar key_char) {
181 return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable,
182 command_key, shift_key,
183 cntrl_key, opt_key, vkey_code,
187 int CommandForBrowserKeyboardShortcut(
188 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
189 int vkey_code, unichar key_char) {
190 return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable,
191 command_key, shift_key,
192 cntrl_key, opt_key, vkey_code,
196 int CommandForKeyEvent(NSEvent* event) {
197 if ([event type] != NSKeyDown)
201 NSMenuItem* item = FindMenuItem(event, [NSApp mainMenu]);
202 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0)
205 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items
206 // that do not correspond to IDC_ constants need no special treatment however,
207 // as they can't be blacklisted in
208 // |BrowserCommandController::IsReservedCommandOrKey()| anyhow.
209 if (item && [item action] == @selector(performClose:))
210 return IDC_CLOSE_WINDOW;
212 // "Exit" doesn't use the |commandDispatch:| mechanism either.
213 if (item && [item action] == @selector(terminate:))
216 // Look in secondary keyboard shortcuts.
217 NSUInteger modifiers = [event modifierFlags];
218 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0;
219 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0;
220 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0;
221 const bool optKey = (modifiers & NSAlternateKeyMask) != 0;
222 const int keyCode = [event keyCode];
223 const unichar keyChar = KeyCharacterForEvent(event);
225 int cmdNum = CommandForWindowKeyboardShortcut(
226 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
230 cmdNum = CommandForBrowserKeyboardShortcut(
231 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
238 unichar KeyCharacterForEvent(NSEvent* event) {
239 NSString* eventString = [event charactersIgnoringModifiers];
240 NSString* characters = [event characters];
242 if ([eventString length] != 1)
245 if ([characters length] != 1)
246 return [eventString characterAtIndex:0];
248 // Some characters are BiDi mirrored. The mirroring is different
249 // for different OS versions. Instead of having a mirror table, map
250 // raw/processed pairs to desired outputs.
256 // OSX 10.8 mirrors certain chars.
262 // OSX 10.9 has the unshifted raw char.
268 // These are the same either way.
273 unichar noModifiersChar = [eventString characterAtIndex:0];
274 unichar rawChar = [characters characterAtIndex:0];
276 // Only apply transformation table for ascii.
277 if (isascii(noModifiersChar) && isascii(rawChar)) {
278 // Alphabetic characters aren't mirrored, go with the raw character.
279 // [A previous partial comment said something about Dvorak?]
280 if (isalpha(rawChar))
283 // http://crbug.com/42517
284 // http://crbug.com/315379
285 // In RTL keyboard layouts, Cocoa mirrors characters in the string
286 // returned by [event charactersIgnoringModifiers]. In this case, return
287 // the raw (unmirrored) char.
288 for (size_t i = 0; i < arraysize(kCharMapping); ++i) {
289 if (rawChar == kCharMapping[i].rawChar &&
290 noModifiersChar == kCharMapping[i].unmodChar) {
291 return kCharMapping[i].targetChar;
295 // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
296 if ([event modifierFlags] & NSAlternateKeyMask)
300 return noModifiersChar;