[email protected] | 0996e9b | 2011-08-26 17:59:01 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
andresantoso | 2389f84 | 2015-03-31 23:26:36 | [diff] [blame^] | 5 | #import <AppKit/AppKit.h> |
[email protected] | b6b0ca4 | 2009-09-18 16:24:24 | [diff] [blame] | 6 | #include <Carbon/Carbon.h> |
| 7 | |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 8 | #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 12 | #include "chrome/app/chrome_command_ids.h" |
andresantoso | 2389f84 | 2015-03-31 23:26:36 | [diff] [blame^] | 13 | #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 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; |
| 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] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 38 | |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 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 |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 45 | // (DelayedWindowKeyboardShortcut). |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 46 | |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 47 | const KeyboardShortcutData* GetWindowKeyboardShortcutTable( |
| 48 | size_t* num_entries) { |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 49 | static const KeyboardShortcutData keyboard_shortcuts[] = { |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 50 | //cmd shift cntrl option |
| 51 | //--- ----- ----- ------ |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 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}, |
[email protected] | b6b0ca4 | 2009-09-18 16:24:24 | [diff] [blame] | 60 | // Cmd-0..8 select the Nth tab, with cmd-9 being "last tab". |
[email protected] | b7b0bcb | 2010-11-17 17:12:24 | [diff] [blame] | 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}, |
[email protected] | 413d24d | 2011-11-28 16:16:05 | [diff] [blame] | 79 | {true, true, false, false, kVK_ANSI_M, 0, IDC_SHOW_AVATAR_MENU}, |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 80 | }; |
[email protected] | 70be00a | 2009-07-08 23:40:08 | [diff] [blame] | 81 | |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 82 | *num_entries = arraysize(keyboard_shortcuts); |
[email protected] | 70be00a | 2009-07-08 23:40:08 | [diff] [blame] | 83 | |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 84 | return keyboard_shortcuts; |
| 85 | } |
| 86 | |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 87 | const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable( |
| 88 | size_t* num_entries) { |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 89 | static const KeyboardShortcutData keyboard_shortcuts[] = { |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 90 | //cmd shift cntrl option |
| 91 | //--- ----- ----- ------ |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 92 | {false, false, false, false, kVK_Escape, 0, IDC_STOP}, |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | *num_entries = arraysize(keyboard_shortcuts); |
| 96 | |
| 97 | return keyboard_shortcuts; |
| 98 | } |
| 99 | |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 100 | const KeyboardShortcutData* GetBrowserKeyboardShortcutTable( |
| 101 | size_t* num_entries) { |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 102 | static const KeyboardShortcutData keyboard_shortcuts[] = { |
[email protected] | 8656ff1f | 2010-11-19 22:18:12 | [diff] [blame] | 103 | //cmd shift cntrl option |
| 104 | //--- ----- ----- ------ |
[email protected] | 2a8a9812 | 2010-07-16 11:58:48 | [diff] [blame] | 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}, |
[email protected] | 9207c59 | 2011-10-03 20:03:12 | [diff] [blame] | 110 | {true, true, false, false, kVK_ANSI_Period, 0, |
| 111 | IDC_TOGGLE_SPEECH_INPUT}, |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | *num_entries = arraysize(keyboard_shortcuts); |
| 115 | |
| 116 | return keyboard_shortcuts; |
| 117 | } |
| 118 | |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 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) |
| 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] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 147 | static int CommandForKeyboardShortcut( |
| 148 | const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*), |
[email protected] | f7378a3 | 2009-10-21 17:15:28 | [diff] [blame] | 149 | bool command_key, bool shift_key, bool cntrl_key, bool opt_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 150 | int vkey_code, unichar key_char) { |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 151 | |
| 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] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 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). |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 159 | size_t num_shortcuts = 0; |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 160 | const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts); |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 161 | for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 162 | if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key, |
| 163 | opt_key, vkey_code, key_char)) |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 164 | return it->chrome_command; |
[email protected] | 3641da6c | 2009-07-08 14:59:06 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return -1; |
| 168 | } |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 169 | |
| 170 | int CommandForWindowKeyboardShortcut( |
[email protected] | f7378a3 | 2009-10-21 17:15:28 | [diff] [blame] | 171 | bool command_key, bool shift_key, bool cntrl_key, bool opt_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 172 | int vkey_code, unichar key_char) { |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 173 | return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable, |
| 174 | command_key, shift_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 175 | cntrl_key, opt_key, vkey_code, |
| 176 | key_char); |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 179 | int CommandForDelayedWindowKeyboardShortcut( |
| 180 | bool command_key, bool shift_key, bool cntrl_key, bool opt_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 181 | int vkey_code, unichar key_char) { |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 182 | return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable, |
| 183 | command_key, shift_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 184 | cntrl_key, opt_key, vkey_code, |
| 185 | key_char); |
[email protected] | 1ade7b6 | 2009-12-18 08:56:18 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 188 | int CommandForBrowserKeyboardShortcut( |
[email protected] | f7378a3 | 2009-10-21 17:15:28 | [diff] [blame] | 189 | bool command_key, bool shift_key, bool cntrl_key, bool opt_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 190 | int vkey_code, unichar key_char) { |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 191 | return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable, |
| 192 | command_key, shift_key, |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 193 | cntrl_key, opt_key, vkey_code, |
| 194 | key_char); |
| 195 | } |
| 196 | |
andresantoso | 2389f84 | 2015-03-31 23:26:36 | [diff] [blame^] | 197 | int 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] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 239 | unichar KeyCharacterForEvent(NSEvent* event) { |
[email protected] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 240 | NSString* eventString = [event charactersIgnoringModifiers]; |
| 241 | NSString* characters = [event characters]; |
| 242 | |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 243 | if ([eventString length] != 1) |
| 244 | return 0; |
| 245 | |
| 246 | if ([characters length] != 1) |
| 247 | return [eventString characterAtIndex:0]; |
| 248 | |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 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. |
| 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] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 274 | unichar noModifiersChar = [eventString characterAtIndex:0]; |
| 275 | unichar rawChar = [characters characterAtIndex:0]; |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 276 | |
| 277 | // Only apply transformation table for ascii. |
[email protected] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 278 | if (isascii(noModifiersChar) && isascii(rawChar)) { |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 279 | // Alphabetic characters aren't mirrored, go with the raw character. |
| 280 | // [A previous partial comment said something about Dvorak?] |
[email protected] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 281 | if (isalpha(rawChar)) |
| 282 | return rawChar; |
| 283 | |
| 284 | // http://crbug.com/42517 |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 285 | // http://crbug.com/315379 |
[email protected] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 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. |
viettrungluu | 9e65ad1 | 2014-10-16 04:22:26 | [diff] [blame] | 289 | for (size_t i = 0; i < arraysize(kCharMapping); ++i) { |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 290 | if (rawChar == kCharMapping[i].rawChar && |
| 291 | noModifiersChar == kCharMapping[i].unmodChar) { |
| 292 | return kCharMapping[i].targetChar; |
| 293 | } |
[email protected] | b881f4f4 | 2010-11-16 08:58:27 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 296 | // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8). |
| 297 | if ([event modifierFlags] & NSAlternateKeyMask) |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 298 | return rawChar; |
[email protected] | 92f5adfa | 2010-01-05 09:49:12 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 147f124 | 2013-12-02 21:52:34 | [diff] [blame] | 301 | return noModifiersChar; |
[email protected] | 1d313b83 | 2009-10-09 01:26:20 | [diff] [blame] | 302 | } |