1 // Copyright (c) 2013 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 "chrome/browser/ui/accelerator_utils.h"
7 #import <Cocoa/Cocoa.h>
9 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
10 #include "chrome/browser/ui/cocoa/accelerators_cocoa.h"
11 #include "ui/base/accelerators/accelerator.h"
12 #import "ui/base/accelerators/platform_accelerator_cocoa.h"
13 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
17 bool IsChromeAccelerator(const ui::Accelerator& accelerator, Profile* profile) {
18 // The |accelerator| passed in contains a Windows key code but no platform
19 // accelerator info. The Accelerator list is the opposite: It has accelerators
20 // that have key_code() == VKEY_UNKNOWN but they contain a platform
21 // accelerator. We find common ground by converting the passed in Windows key
22 // code to a character and use that when comparing against the Accelerator
24 unichar shifted_character;
25 ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), 0, &shifted_character,
27 NSString* characters =
28 [[[NSString alloc] initWithCharacters:&shifted_character
29 length:1] autorelease];
31 NSUInteger modifiers =
32 (accelerator.IsCtrlDown() ? NSControlKeyMask : 0) |
33 (accelerator.IsCmdDown() ? NSCommandKeyMask : 0) |
34 (accelerator.IsAltDown() ? NSAlternateKeyMask : 0) |
35 (accelerator.IsShiftDown() ? NSShiftKeyMask : 0);
37 NSEvent* event = [NSEvent keyEventWithType:NSKeyDown
39 modifierFlags:modifiers
44 charactersIgnoringModifiers:characters
46 keyCode:accelerator.key_code()];
48 return CommandForKeyEvent(event) != -1;
51 ui::Accelerator GetPrimaryChromeAcceleratorForCommandId(int command_id) {
52 const ui::Accelerator* accelerator =
53 AcceleratorsCocoa::GetInstance()->GetAcceleratorForCommand(command_id);
55 return accelerator ? *accelerator : ui::Accelerator();