godot_application.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**************************************************************************/
  2. /* godot_application.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "godot_application.h"
  31. #include "display_server_macos.h"
  32. @implementation GodotApplication
  33. - (void)mediaKeyEvent:(int)key state:(BOOL)state repeat:(BOOL)repeat {
  34. Key keycode = Key::NONE;
  35. switch (key) {
  36. case NX_KEYTYPE_SOUND_UP: {
  37. keycode = Key::VOLUMEUP;
  38. } break;
  39. case NX_KEYTYPE_SOUND_DOWN: {
  40. keycode = Key::VOLUMEUP;
  41. } break;
  42. //NX_KEYTYPE_BRIGHTNESS_UP
  43. //NX_KEYTYPE_BRIGHTNESS_DOWN
  44. case NX_KEYTYPE_CAPS_LOCK: {
  45. keycode = Key::CAPSLOCK;
  46. } break;
  47. case NX_KEYTYPE_HELP: {
  48. keycode = Key::HELP;
  49. } break;
  50. case NX_POWER_KEY: {
  51. keycode = Key::STANDBY;
  52. } break;
  53. case NX_KEYTYPE_MUTE: {
  54. keycode = Key::VOLUMEMUTE;
  55. } break;
  56. //NX_KEYTYPE_CONTRAST_UP
  57. //NX_KEYTYPE_CONTRAST_DOWN
  58. //NX_KEYTYPE_LAUNCH_PANEL
  59. //NX_KEYTYPE_EJECT
  60. //NX_KEYTYPE_VIDMIRROR
  61. //NX_KEYTYPE_FAST
  62. //NX_KEYTYPE_REWIND
  63. //NX_KEYTYPE_ILLUMINATION_UP
  64. //NX_KEYTYPE_ILLUMINATION_DOWN
  65. //NX_KEYTYPE_ILLUMINATION_TOGGLE
  66. case NX_KEYTYPE_PLAY: {
  67. keycode = Key::MEDIAPLAY;
  68. } break;
  69. case NX_KEYTYPE_NEXT: {
  70. keycode = Key::MEDIANEXT;
  71. } break;
  72. case NX_KEYTYPE_PREVIOUS: {
  73. keycode = Key::MEDIAPREVIOUS;
  74. } break;
  75. default: {
  76. keycode = Key::NONE;
  77. } break;
  78. }
  79. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  80. if (ds && keycode != Key::NONE) {
  81. DisplayServerMacOS::KeyEvent ke;
  82. ke.window_id = ds->_get_focused_window_or_popup();
  83. ke.macos_state = 0;
  84. ke.pressed = state;
  85. ke.echo = repeat;
  86. ke.keycode = keycode;
  87. ke.physical_keycode = keycode;
  88. ke.key_label = keycode;
  89. ke.unicode = 0;
  90. ke.raw = true;
  91. ds->push_to_key_event_buffer(ke);
  92. }
  93. }
  94. - (void)sendEvent:(NSEvent *)event {
  95. if ([event type] == NSEventTypeSystemDefined && [event subtype] == 8) {
  96. int keyCode = (([event data1] & 0xFFFF0000) >> 16);
  97. int keyFlags = ([event data1] & 0x0000FFFF);
  98. int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
  99. int keyRepeat = (keyFlags & 0x1);
  100. [self mediaKeyEvent:keyCode state:keyState repeat:keyRepeat];
  101. }
  102. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  103. if (ds) {
  104. if ([event type] == NSEventTypeLeftMouseDown || [event type] == NSEventTypeRightMouseDown || [event type] == NSEventTypeOtherMouseDown) {
  105. if (ds->mouse_process_popups()) {
  106. return;
  107. }
  108. }
  109. ds->send_event(event);
  110. }
  111. // From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost
  112. // This works around an AppKit bug, where key up events while holding
  113. // down the command key don't get sent to the key window.
  114. if ([event type] == NSEventTypeKeyUp && ([event modifierFlags] & NSEventModifierFlagCommand)) {
  115. [[self keyWindow] sendEvent:event];
  116. } else {
  117. [super sendEvent:event];
  118. }
  119. }
  120. @end