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 #import <AppKit/AppKit.h>
6 #include <Carbon/Carbon.h>
8 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h"
17 // Returns the menu item associated with |key| in |menu|, or nil if not found.
18 NSMenuItem* FindMenuItem(NSEvent* key, NSMenu* menu) {
19 NSMenuItem* result = nil;
21 for (NSMenuItem* item in [menu itemArray]) {
22 NSMenu* submenu = [item submenu];
24 if (submenu != [NSApp servicesMenu])
25 result = FindMenuItem(key, submenu);
26 } else if ([item cr_firesForKeyEventIfEnabled:key]) {
39 // 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
45 // (DelayedWindowKeyboardShortcut).
47 const KeyboardShortcutData* GetWindowKeyboardShortcutTable(
48 size_t* num_entries) {
49 static const KeyboardShortcutData keyboard_shortcuts[] = {
50 //cmd shift cntrl option
51 //--- ----- ----- ------
52 // '{' / '}' 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},
60 // Cmd-0..8 select the Nth tab, with cmd-9 being "last tab".
61 {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},
79 {true, true, false, false, kVK_ANSI_M, 0, IDC_SHOW_AVATAR_MENU},
82 *num_entries = arraysize(keyboard_shortcuts);
84 return keyboard_shortcuts;
87 const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable(
88 size_t* num_entries) {
89 static const KeyboardShortcutData keyboard_shortcuts[] = {
90 //cmd shift cntrl option
91 //--- ----- ----- ------
92 {false, false, false, false, kVK_Escape, 0, IDC_STOP},
95 *num_entries = arraysize(keyboard_shortcuts);
97 return keyboard_shortcuts;
100 const KeyboardShortcutData* GetBrowserKeyboardShortcutTable(
101 size_t* num_entries) {
102 static const KeyboardShortcutData keyboard_shortcuts[] = {
103 //cmd shift cntrl option
104 //--- ----- ----- ------
105 {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},
110 {true, true, false, false, kVK_ANSI_Period, 0,
111 IDC_TOGGLE_SPEECH_INPUT},
114 *num_entries = arraysize(keyboard_shortcuts);
116 return keyboard_shortcuts;
119 static 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)
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)
142 NOTREACHED(); // Shouldn't happen.
147 static int CommandForKeyboardShortcut(
148 const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*),
149 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
150 int vkey_code, unichar key_char) {
152 // Scan through keycodes and see if it corresponds to one of the global
153 // shortcuts on file.
155 // TODO(jeremy): Change this into a hash table once we get enough
156 // entries in the array to make a difference.
157 // (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).
159 size_t num_shortcuts = 0;
160 const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts);
161 for (size_t i = 0; i < num_shortcuts; ++i, ++it) {
162 if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key,
163 opt_key, vkey_code, key_char))
164 return it->chrome_command;
170 int CommandForWindowKeyboardShortcut(
171 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
172 int vkey_code, unichar key_char) {
173 return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable,
174 command_key, shift_key,
175 cntrl_key, opt_key, vkey_code,
179 int CommandForDelayedWindowKeyboardShortcut(
180 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
181 int vkey_code, unichar key_char) {
182 return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable,
183 command_key, shift_key,
184 cntrl_key, opt_key, vkey_code,
188 int CommandForBrowserKeyboardShortcut(
189 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
190 int vkey_code, unichar key_char) {
191 return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable,
192 command_key, shift_key,
193 cntrl_key, opt_key, vkey_code,
197 int CommandForKeyEvent(NSEvent* event) {
198 if ([event type] != NSKeyDown)
202 NSMenuItem* item = FindMenuItem(event, [NSApp mainMenu]);
203 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0)
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;
213 // "Exit" doesn't use the |commandDispatch:| mechanism either.
214 if (item && [item action] == @selector(terminate:))
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);
226 int cmdNum = CommandForWindowKeyboardShortcut(
227 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
231 cmdNum = CommandForBrowserKeyboardShortcut(
232 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar);
239 unichar KeyCharacterForEvent(NSEvent* event) {
240 NSString* eventString = [event charactersIgnoringModifiers];
241 NSString* characters = [event characters];
243 if ([eventString length] != 1)
246 if ([characters length] != 1)
247 return [eventString characterAtIndex:0];
249 // 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.
257 // OSX 10.8 mirrors certain chars.
263 // OSX 10.9 has the unshifted raw char.
269 // These are the same either way.
274 unichar noModifiersChar = [eventString characterAtIndex:0];
275 unichar rawChar = [characters characterAtIndex:0];
277 // Only apply transformation table for ascii.
278 if (isascii(noModifiersChar) && isascii(rawChar)) {
279 // Alphabetic characters aren't mirrored, go with the raw character.
280 // [A previous partial comment said something about Dvorak?]
281 if (isalpha(rawChar))
284 // http://crbug.com/42517
285 // http://crbug.com/315379
286 // In RTL keyboard layouts, Cocoa mirrors characters in the string
287 // returned by [event charactersIgnoringModifiers]. In this case, return
288 // the raw (unmirrored) char.
289 for (size_t i = 0; i < arraysize(kCharMapping); ++i) {
290 if (rawChar == kCharMapping[i].rawChar &&
291 noModifiersChar == kCharMapping[i].unmodChar) {
292 return kCharMapping[i].targetChar;
296 // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
297 if ([event modifierFlags] & NSAlternateKeyMask)
301 return noModifiersChar;