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 <AppKit/NSEvent.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"
14 // Basically, there are two kinds of keyboard shortcuts: Ones that should work
15 // only if the tab contents is focused (BrowserKeyboardShortcut), and ones that
16 // should work in all other cases (WindowKeyboardShortcut). In the latter case,
17 // we differentiate between shortcuts that are checked before any other view
18 // gets the chance to handle them (WindowKeyboardShortcut) or after all views
19 // had a chance but did not handle the keypress event
20 // (DelayedWindowKeyboardShortcut).
22 const KeyboardShortcutData* GetWindowKeyboardShortcutTable(
23 size_t* num_entries) {
24 static const KeyboardShortcutData keyboard_shortcuts[] = {
25 //cmd shift cntrl option
26 //--- ----- ----- ------
27 // '{' / '}' characters should be matched earlier than virtual key code
28 // (therefore we can match alt-8 as '{' on german keyboards).
29 {true, false, false, false, 0, '}', IDC_SELECT_NEXT_TAB},
30 {true, false, false, false, 0, '{', IDC_SELECT_PREVIOUS_TAB},
31 {false, false, true, false, kVK_PageDown, 0, IDC_SELECT_NEXT_TAB},
32 {false, false, true, false, kVK_Tab, 0, IDC_SELECT_NEXT_TAB},
33 {false, false, true, false, kVK_PageUp, 0, IDC_SELECT_PREVIOUS_TAB},
34 {false, true, true, false, kVK_Tab, 0, IDC_SELECT_PREVIOUS_TAB},
35 // Cmd-0..8 select the Nth tab, with cmd-9 being "last tab".
36 {true, false, false, false, kVK_ANSI_1, 0, IDC_SELECT_TAB_0},
37 {true, false, false, false, kVK_ANSI_Keypad1, 0, IDC_SELECT_TAB_0},
38 {true, false, false, false, kVK_ANSI_2, 0, IDC_SELECT_TAB_1},
39 {true, false, false, false, kVK_ANSI_Keypad2, 0, IDC_SELECT_TAB_1},
40 {true, false, false, false, kVK_ANSI_3, 0, IDC_SELECT_TAB_2},
41 {true, false, false, false, kVK_ANSI_Keypad3, 0, IDC_SELECT_TAB_2},
42 {true, false, false, false, kVK_ANSI_4, 0, IDC_SELECT_TAB_3},
43 {true, false, false, false, kVK_ANSI_Keypad4, 0, IDC_SELECT_TAB_3},
44 {true, false, false, false, kVK_ANSI_5, 0, IDC_SELECT_TAB_4},
45 {true, false, false, false, kVK_ANSI_Keypad5, 0, IDC_SELECT_TAB_4},
46 {true, false, false, false, kVK_ANSI_6, 0, IDC_SELECT_TAB_5},
47 {true, false, false, false, kVK_ANSI_Keypad6, 0, IDC_SELECT_TAB_5},
48 {true, false, false, false, kVK_ANSI_7, 0, IDC_SELECT_TAB_6},
49 {true, false, false, false, kVK_ANSI_Keypad7, 0, IDC_SELECT_TAB_6},
50 {true, false, false, false, kVK_ANSI_8, 0, IDC_SELECT_TAB_7},
51 {true, false, false, false, kVK_ANSI_Keypad8, 0, IDC_SELECT_TAB_7},
52 {true, false, false, false, kVK_ANSI_9, 0, IDC_SELECT_LAST_TAB},
53 {true, false, false, false, kVK_ANSI_Keypad9, 0, IDC_SELECT_LAST_TAB},
54 {true, true, false, false, kVK_ANSI_M, 0, IDC_SHOW_AVATAR_MENU},
57 *num_entries = arraysize(keyboard_shortcuts);
59 return keyboard_shortcuts;
62 const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable(
63 size_t* num_entries) {
64 static const KeyboardShortcutData keyboard_shortcuts[] = {
65 //cmd shift cntrl option
66 //--- ----- ----- ------
67 {false, false, false, false, kVK_Escape, 0, IDC_STOP},
70 *num_entries = arraysize(keyboard_shortcuts);
72 return keyboard_shortcuts;
75 const KeyboardShortcutData* GetBrowserKeyboardShortcutTable(
76 size_t* num_entries) {
77 static const KeyboardShortcutData keyboard_shortcuts[] = {
78 //cmd shift cntrl option
79 //--- ----- ----- ------
80 {true, false, false, false, kVK_LeftArrow, 0, IDC_BACK},
81 {true, false, false, false, kVK_RightArrow, 0, IDC_FORWARD},
82 {false, false, false, false, kVK_Delete, 0, IDC_BACK},
83 {false, true, false, false, kVK_Delete, 0, IDC_FORWARD},
84 {true, true, false, false, 0, 'c', IDC_DEV_TOOLS_INSPECT},
85 {true, true, false, false, kVK_ANSI_Period, 0,
86 IDC_TOGGLE_SPEECH_INPUT},
89 *num_entries = arraysize(keyboard_shortcuts);
91 return keyboard_shortcuts;
94 static bool MatchesEventForKeyboardShortcut(
95 const KeyboardShortcutData& shortcut,
96 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
97 int vkey_code, unichar key_char) {
98 // Expects that one of |key_char| or |vkey_code| is 0.
99 DCHECK((shortcut.key_char == 0) ^ (shortcut.vkey_code == 0));
100 if (shortcut.key_char) {
101 // The given shortcut key is to be matched by a keyboard character.
102 // In this case we ignore shift and opt (alt) key modifiers, because
103 // the character may be generated by a combination with those keys.
104 if (shortcut.command_key == command_key &&
105 shortcut.cntrl_key == cntrl_key &&
106 shortcut.key_char == key_char)
108 } else if (shortcut.vkey_code) {
109 // The given shortcut key is to be matched by a virtual key code.
110 if (shortcut.command_key == command_key &&
111 shortcut.shift_key == shift_key &&
112 shortcut.cntrl_key == cntrl_key &&
113 shortcut.opt_key == opt_key &&
114 shortcut.vkey_code == vkey_code)
117 NOTREACHED(); // Shouldn't happen.
122 static int CommandForKeyboardShortcut(
123 const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*),
124 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
125 int vkey_code, unichar key_char) {
127 // Scan through keycodes and see if it corresponds to one of the global
128 // shortcuts on file.
130 // TODO(jeremy): Change this into a hash table once we get enough
131 // entries in the array to make a difference.
132 // (When turning this into a hash table, note that the current behavior
133 // relies on the order of the table (see the comment for '{' / '}' above).
134 size_t num_shortcuts = 0;
135 const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts);
136 for (size_t i = 0; i < num_shortcuts; ++i, ++it) {
137 if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key,
138 opt_key, vkey_code, key_char))
139 return it->chrome_command;
145 int CommandForWindowKeyboardShortcut(
146 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
147 int vkey_code, unichar key_char) {
148 return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable,
149 command_key, shift_key,
150 cntrl_key, opt_key, vkey_code,
154 int CommandForDelayedWindowKeyboardShortcut(
155 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
156 int vkey_code, unichar key_char) {
157 return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable,
158 command_key, shift_key,
159 cntrl_key, opt_key, vkey_code,
163 int CommandForBrowserKeyboardShortcut(
164 bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
165 int vkey_code, unichar key_char) {
166 return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable,
167 command_key, shift_key,
168 cntrl_key, opt_key, vkey_code,
172 unichar KeyCharacterForEvent(NSEvent* event) {
173 NSString* eventString = [event charactersIgnoringModifiers];
174 NSString* characters = [event characters];
176 // Character pairs that undergo BiDi mirrored.
177 // There are actually many more such pairs, but these are the ones that
178 // are likely to show up in keyboard shortcuts.
182 } kMirroredBiDiChars[] = {
188 if ([eventString length] != 1)
191 if ([characters length] != 1)
192 return [eventString characterAtIndex:0];
194 unichar noModifiersChar = [eventString characterAtIndex:0];
195 unichar rawChar = [characters characterAtIndex:0];
196 // When both |characters| and |charactersIgnoringModifiers| are ascii,
197 // return the first character of |characters|, if...
198 if (isascii(noModifiersChar) && isascii(rawChar)) {
199 // |characters| is an alphabet (mainly for dvorak-qwerty layout), or
200 if (isalpha(rawChar))
203 // http://crbug.com/42517
204 // In RTL keyboard layouts, Cocoa mirrors characters in the string
205 // returned by [event charactersIgnoringModifiers]. In this case, return
206 // the raw (unmirrored) char.
207 // FIXME: If there is a need to add any more characters to the
208 // kMirroredBiDiChars table, then it's probably better to use ICU's
209 // u_charMirror() function to perform this test.
210 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kMirroredBiDiChars); ++i) {
211 const unichar& a = kMirroredBiDiChars[i].a;
212 const unichar& b = kMirroredBiDiChars[i].b;
213 if ((rawChar == a && noModifiersChar == b) ||
214 (rawChar == b && noModifiersChar == a))
218 // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
219 if ([event modifierFlags] & NSAlternateKeyMask)
220 return [characters characterAtIndex:0];
223 return [eventString characterAtIndex:0];