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 if ([eventString length] != 1)
179 if ([characters length] != 1)
180 return [eventString characterAtIndex:0];
182 // Some characters are BiDi mirrored. The mirroring is different
183 // for different OS versions. Instead of having a mirror table, map
184 // raw/processed pairs to desired outputs.
190 // OSX 10.8 mirrors certain chars.
196 // OSX 10.9 has the unshifted raw char.
202 // These are the same either way.
207 unichar noModifiersChar = [eventString characterAtIndex:0];
208 unichar rawChar = [characters characterAtIndex:0];
210 // Only apply transformation table for ascii.
211 if (isascii(noModifiersChar) && isascii(rawChar)) {
212 // Alphabetic characters aren't mirrored, go with the raw character.
213 // [A previous partial comment said something about Dvorak?]
214 if (isalpha(rawChar))
217 // http://crbug.com/42517
218 // http://crbug.com/315379
219 // In RTL keyboard layouts, Cocoa mirrors characters in the string
220 // returned by [event charactersIgnoringModifiers]. In this case, return
221 // the raw (unmirrored) char.
222 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kCharMapping); ++i) {
223 if (rawChar == kCharMapping[i].rawChar &&
224 noModifiersChar == kCharMapping[i].unmodChar) {
225 return kCharMapping[i].targetChar;
229 // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
230 if ([event modifierFlags] & NSAlternateKeyMask)
234 return noModifiersChar;