accelerator_util_mac.mm 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/accelerator_util.h"
  5. #include "ui/base/accelerators/accelerator.h"
  6. #import "ui/base/accelerators/platform_accelerator_cocoa.h"
  7. #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
  8. namespace accelerator_util {
  9. void SetPlatformAccelerator(ui::Accelerator* accelerator) {
  10. unichar character;
  11. unichar characterIgnoringModifiers;
  12. NSUInteger modifiers = (accelerator->IsCtrlDown() ? NSControlKeyMask : 0) |
  13. (accelerator->IsCmdDown() ? NSCommandKeyMask : 0) |
  14. (accelerator->IsAltDown() ? NSAlternateKeyMask : 0) |
  15. (accelerator->IsShiftDown() ? NSShiftKeyMask : 0);
  16. ui::MacKeyCodeForWindowsKeyCode(accelerator->key_code(), modifiers,
  17. &character, &characterIgnoringModifiers);
  18. if (character != characterIgnoringModifiers) {
  19. if (isdigit(characterIgnoringModifiers)) {
  20. // The character is a number so lets not mutate it with the modifiers
  21. character = characterIgnoringModifiers;
  22. } else {
  23. modifiers ^= NSShiftKeyMask;
  24. }
  25. }
  26. if (character == NSDeleteFunctionKey) {
  27. character = NSDeleteCharacter;
  28. }
  29. NSString* characters =
  30. [[[NSString alloc] initWithCharacters:&character length:1] autorelease];
  31. std::unique_ptr<ui::PlatformAccelerator> platform_accelerator(
  32. new ui::PlatformAcceleratorCocoa(characters, modifiers));
  33. accelerator->set_platform_accelerator(std::move(platform_accelerator));
  34. }
  35. } // namespace accelerator_util