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/profiles/profile.h"
6 #include "chrome/browser/ui/cocoa/accelerators_cocoa.h"
7 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
8 #include "content/public/browser/native_web_keyboard_event.h"
9 #include "ui/base/accelerators/accelerator.h"
10 #import "ui/base/accelerators/platform_accelerator_cocoa.h"
11 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
15 bool IsChromeAccelerator(const ui::Accelerator& accelerator, Profile* profile) {
16 // The |accelerator| passed in contains a Windows key code but no platform
17 // accelerator info. The Accelerator list is the opposite: It has accelerators
18 // that have key_code() == VKEY_UNKNOWN but they contain a platform
19 // accelerator. We find common ground by converting the passed in Windows key
20 // code to a character and use that when comparing against the Accelerator
23 unichar characterIgnoringModifiers;
24 ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(),
27 &characterIgnoringModifiers);
28 NSString* characters =
29 [[[NSString alloc] initWithCharacters:&character 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 content::NativeWebKeyboardEvent keyboard_event(event);
49 int id = [BrowserWindowUtils getCommandId:keyboard_event];
53 ui::Accelerator GetPrimaryChromeAcceleratorForCommandId(int command_id) {
54 const ui::Accelerator* accelerator =
55 AcceleratorsCocoa::GetInstance()->GetAcceleratorForCommand(command_id);
57 return accelerator ? *accelerator : ui::Accelerator();