Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Unified Diff: chrome/browser/global_keyboard_shortcuts_mac.mm

Issue 503080: Make cmd-{/} shortcut keys work on any keyboard layouts on Mac (Closed)
Patch Set: rename fix Created 10 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/global_keyboard_shortcuts_mac.mm
diff --git a/chrome/browser/global_keyboard_shortcuts_mac.mm b/chrome/browser/global_keyboard_shortcuts_mac.mm
index 4872a40e895fcdc1cd964a37958d4b67d3dc819d..f40e91cb24f5920fb4033221644950d48f35e0bc 100644
--- a/chrome/browser/global_keyboard_shortcuts_mac.mm
+++ b/chrome/browser/global_keyboard_shortcuts_mac.mm
@@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <AppKit/NSEvent.h>
#include <Carbon/Carbon.h>
#include "chrome/browser/global_keyboard_shortcuts_mac.h"
#include "base/basictypes.h"
+#include "base/logging.h"
#include "chrome/app/chrome_dll_resource.h"
// Basically, there are two kinds of keyboard shortcuts: Ones that should work
@@ -20,22 +22,24 @@
const KeyboardShortcutData* GetWindowKeyboardShortcutTable
(size_t* num_entries) {
static const KeyboardShortcutData keyboard_shortcuts[] = {
- {true, true, false, false, kVK_ANSI_RightBracket, IDC_SELECT_NEXT_TAB},
- {false, false, true, false, kVK_PageDown, IDC_SELECT_NEXT_TAB},
- {false, false, true, false, kVK_Tab, IDC_SELECT_NEXT_TAB},
- {true, true, false, false, kVK_ANSI_LeftBracket, IDC_SELECT_PREVIOUS_TAB},
- {false, false, true, false, kVK_PageUp, IDC_SELECT_PREVIOUS_TAB},
- {false, true, true, false, kVK_Tab, IDC_SELECT_PREVIOUS_TAB},
+ // '{' / '}' characters should be matched earlier than virtual key code
+ // (therefore we can match alt-8 as '{' on german keyboards).
+ {true, false, false, false, 0, '}', IDC_SELECT_NEXT_TAB},
+ {true, false, false, false, 0, '{', IDC_SELECT_PREVIOUS_TAB},
+ {false, false, true, false, kVK_PageDown, 0, IDC_SELECT_NEXT_TAB},
+ {false, false, true, false, kVK_Tab, 0, IDC_SELECT_NEXT_TAB},
+ {false, false, true, false, kVK_PageUp, 0, IDC_SELECT_PREVIOUS_TAB},
+ {false, true, true, false, kVK_Tab, 0, IDC_SELECT_PREVIOUS_TAB},
// Cmd-0..8 select the Nth tab, with cmd-9 being "last tab".
- {true, false, false, false, kVK_ANSI_1, IDC_SELECT_TAB_0},
- {true, false, false, false, kVK_ANSI_2, IDC_SELECT_TAB_1},
- {true, false, false, false, kVK_ANSI_3, IDC_SELECT_TAB_2},
- {true, false, false, false, kVK_ANSI_4, IDC_SELECT_TAB_3},
- {true, false, false, false, kVK_ANSI_5, IDC_SELECT_TAB_4},
- {true, false, false, false, kVK_ANSI_6, IDC_SELECT_TAB_5},
- {true, false, false, false, kVK_ANSI_7, IDC_SELECT_TAB_6},
- {true, false, false, false, kVK_ANSI_8, IDC_SELECT_TAB_7},
- {true, false, false, false, kVK_ANSI_9, IDC_SELECT_LAST_TAB},
+ {true, false, false, false, kVK_ANSI_1, 0, IDC_SELECT_TAB_0},
+ {true, false, false, false, kVK_ANSI_2, 0, IDC_SELECT_TAB_1},
+ {true, false, false, false, kVK_ANSI_3, 0, IDC_SELECT_TAB_2},
+ {true, false, false, false, kVK_ANSI_4, 0, IDC_SELECT_TAB_3},
+ {true, false, false, false, kVK_ANSI_5, 0, IDC_SELECT_TAB_4},
+ {true, false, false, false, kVK_ANSI_6, 0, IDC_SELECT_TAB_5},
+ {true, false, false, false, kVK_ANSI_7, 0, IDC_SELECT_TAB_6},
+ {true, false, false, false, kVK_ANSI_8, 0, IDC_SELECT_TAB_7},
+ {true, false, false, false, kVK_ANSI_9, 0, IDC_SELECT_LAST_TAB},
};
*num_entries = arraysize(keyboard_shortcuts);
@@ -46,7 +50,7 @@ const KeyboardShortcutData* GetWindowKeyboardShortcutTable
const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable
(size_t* num_entries) {
static const KeyboardShortcutData keyboard_shortcuts[] = {
- {false, false, false, false, kVK_Escape, IDC_STOP},
+ {false, false, false, false, kVK_Escape, 0, IDC_STOP},
};
*num_entries = arraysize(keyboard_shortcuts);
@@ -57,10 +61,10 @@ const KeyboardShortcutData* GetDelayedWindowKeyboardShortcutTable
const KeyboardShortcutData* GetBrowserKeyboardShortcutTable
(size_t* num_entries) {
static const KeyboardShortcutData keyboard_shortcuts[] = {
- {true, false, false, false, kVK_LeftArrow, IDC_BACK},
- {true, false, false, false, kVK_RightArrow, IDC_FORWARD},
- {false, false, false, false, kVK_Delete, IDC_BACK},
- {false, true, false, false, kVK_Delete, IDC_FORWARD},
+ {true, false, false, false, kVK_LeftArrow, 0, IDC_BACK},
+ {true, false, false, false, kVK_RightArrow, 0, IDC_FORWARD},
+ {false, false, false, false, kVK_Delete, 0, IDC_BACK},
+ {false, true, false, false, kVK_Delete, 0, IDC_FORWARD},
};
*num_entries = arraysize(keyboard_shortcuts);
@@ -68,26 +72,52 @@ const KeyboardShortcutData* GetBrowserKeyboardShortcutTable
return keyboard_shortcuts;
}
+static bool MatchesEventForKeyboardShortcut(
+ const KeyboardShortcutData& shortcut,
+ bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
+ int vkey_code, unichar key_char) {
+ // Expects that one of |key_char| or |vkey_code| is 0.
+ DCHECK((shortcut.key_char == 0) ^ (shortcut.vkey_code == 0));
+ if (shortcut.key_char) {
+ // The given shortcut key is to be matched by a keyboard character.
+ // In this case we ignore shift and opt (alt) key modifiers, because
+ // the character may be generated by a combination with those keys.
+ if (shortcut.command_key == command_key &&
+ shortcut.cntrl_key == cntrl_key &&
+ shortcut.key_char == key_char)
+ return true;
+ } else if (shortcut.vkey_code) {
+ // The given shortcut key is to be matched by a virtual key code.
+ if (shortcut.command_key == command_key &&
+ shortcut.shift_key == shift_key &&
+ shortcut.cntrl_key == cntrl_key &&
+ shortcut.opt_key == opt_key &&
+ shortcut.vkey_code == vkey_code)
+ return true;
+ } else {
+ NOTREACHED(); // Shouldn't happen.
+ }
+ return false;
+}
+
static int CommandForKeyboardShortcut(
const KeyboardShortcutData* (*get_keyboard_shortcut_table)(size_t*),
bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
- int vkey_code) {
+ int vkey_code, unichar key_char) {
// Scan through keycodes and see if it corresponds to one of the global
// shortcuts on file.
//
// TODO(jeremy): Change this into a hash table once we get enough
// entries in the array to make a difference.
+ // (When turning this into a hash table, note that the current behavior
+ // relies on the order of the table (see the comment for '{' / '}' above).
size_t num_shortcuts = 0;
const KeyboardShortcutData *it = get_keyboard_shortcut_table(&num_shortcuts);
for (size_t i = 0; i < num_shortcuts; ++i, ++it) {
- if (it->command_key == command_key &&
- it->shift_key == shift_key &&
- it->cntrl_key == cntrl_key &&
- it->opt_key == opt_key &&
- it->vkey_code == vkey_code) {
+ if (MatchesEventForKeyboardShortcut(*it, command_key, shift_key, cntrl_key,
+ opt_key, vkey_code, key_char))
return it->chrome_command;
- }
}
return -1;
@@ -95,24 +125,52 @@ static int CommandForKeyboardShortcut(
int CommandForWindowKeyboardShortcut(
bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
- int vkey_code) {
+ int vkey_code, unichar key_char) {
return CommandForKeyboardShortcut(GetWindowKeyboardShortcutTable,
command_key, shift_key,
- cntrl_key, opt_key, vkey_code);
+ cntrl_key, opt_key, vkey_code,
+ key_char);
}
int CommandForDelayedWindowKeyboardShortcut(
bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
- int vkey_code) {
+ int vkey_code, unichar key_char) {
return CommandForKeyboardShortcut(GetDelayedWindowKeyboardShortcutTable,
command_key, shift_key,
- cntrl_key, opt_key, vkey_code);
+ cntrl_key, opt_key, vkey_code,
+ key_char);
}
int CommandForBrowserKeyboardShortcut(
bool command_key, bool shift_key, bool cntrl_key, bool opt_key,
- int vkey_code) {
+ int vkey_code, unichar key_char) {
return CommandForKeyboardShortcut(GetBrowserKeyboardShortcutTable,
command_key, shift_key,
- cntrl_key, opt_key, vkey_code);
+ cntrl_key, opt_key, vkey_code,
+ key_char);
+}
+
+unichar KeyCharacterForEvent(NSEvent* event) {
+ const NSString* eventString = [event charactersIgnoringModifiers];
+ const NSString* characters = [event characters];
+
+ if ([eventString length] != 1)
+ return 0;
+
+ if ([characters length] != 1)
+ return [eventString characterAtIndex:0];
+
+ // When both |characters| and |charactersIgnoringModifiers| are ascii,
+ // return the first character of |characters|, if...
+ if (isascii([eventString characterAtIndex:0]) &&
+ isascii([characters characterAtIndex:0])) {
+ // |characters| is an alphabet (mainly for dvorak-qwerty layout), or
+ if (isalpha([characters characterAtIndex:0]))
+ return [characters characterAtIndex:0];
+ // opt/alt modifier is set (e.g. on german layout we want '{' for opt-8).
+ if ([event modifierFlags] & NSAlternateKeyMask)
+ return [characters characterAtIndex:0];
+ }
+
+ return [eventString characterAtIndex:0];
}
« no previous file with comments | « chrome/browser/global_keyboard_shortcuts_mac.h ('k') | chrome/browser/global_keyboard_shortcuts_mac_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698