WebKeyboardMac.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef ATOMIC_PLATFORM_OSX
  23. #include <include/cef_client.h>
  24. #include <ThirdParty/SDL/include/SDL.h>
  25. #include <ThirdParty/SDL/include/SDL_syswm.h>
  26. #include <ThirdParty/SDL/src/events/scancodes_darwin.h>
  27. #include <Atomic/Core/Variant.h>
  28. #include <Atomic/Input/InputEvents.h>
  29. #include <Atomic/Input/Input.h>
  30. #include <Atomic/IO/Log.h>
  31. #include "WebKeyboard.h"
  32. namespace Atomic
  33. {
  34. static bool SDLScanCodeToDarwinScanCode(SDL_Scancode code, int& darwinScanCode)
  35. {
  36. darwinScanCode = -1;
  37. //if (code >= SDL_SCANCODE_A && code <= SDL_SCANCODE_0)
  38. // return false;
  39. int numCodes = sizeof(darwin_scancode_table)/sizeof(SDL_Scancode);
  40. for (int i = 0; i < numCodes; i++)
  41. {
  42. if (darwin_scancode_table[i] == code)
  43. {
  44. darwinScanCode = i;
  45. break;
  46. }
  47. }
  48. if (darwinScanCode != -1)
  49. return true;
  50. return false;
  51. }
  52. bool ConvertKeyEvent(Input* input, const StringHash eventType, VariantMap& eventData, CefKeyEvent& keyEvent)
  53. {
  54. if (eventType != "KeyDown" && eventType != "KeyUp")
  55. {
  56. ATOMIC_LOGERROR("ConvertKeyEvent - Unknown event type");
  57. return false;
  58. }
  59. if (eventType == "KeyUp")
  60. return false;
  61. memset((void*)&keyEvent, 0, sizeof(keyEvent));
  62. WebKeyEvent wk(eventType, eventData);
  63. if (wk.scanCode == SDL_SCANCODE_RETURN)
  64. {
  65. if (!wk.keyDown)
  66. return false;
  67. keyEvent.native_key_code = 36;
  68. keyEvent.type = KEYEVENT_RAWKEYDOWN;
  69. return true;
  70. }
  71. keyEvent.modifiers = EVENTFLAG_NONE;
  72. if (wk.qual & QUAL_SHIFT)
  73. keyEvent.modifiers |= EVENTFLAG_SHIFT_DOWN;
  74. if (wk.qual & QUAL_ALT)
  75. keyEvent.modifiers |= EVENTFLAG_ALT_DOWN;
  76. if (wk.qual & QUAL_CTRL)
  77. keyEvent.modifiers |= EVENTFLAG_CONTROL_DOWN;
  78. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  79. if (!superdown && eventData.Contains("ForceSuperDown"))
  80. {
  81. superdown = eventData["ForceSuperDown"].GetBool();
  82. }
  83. if (superdown)
  84. keyEvent.modifiers |= EVENTFLAG_COMMAND_DOWN;
  85. int darwinScanCode;
  86. if (SDLScanCodeToDarwinScanCode((SDL_Scancode) wk.scanCode, darwinScanCode))
  87. {
  88. keyEvent.native_key_code = darwinScanCode;
  89. keyEvent.type = wk.keyDown ? KEYEVENT_RAWKEYDOWN : KEYEVENT_KEYUP;
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool ConvertTextInputEvent(StringHash eventType, VariantMap& eventData, CefKeyEvent& keyEvent)
  95. {
  96. if (eventType != "TextInput")
  97. {
  98. ATOMIC_LOGERROR("ConvertTextInputEvent - Unknown event type");
  99. return false;
  100. }
  101. String text = eventData[TextInput::P_TEXT].GetString();
  102. SDL_Keycode keyCode = SDL_GetKeyFromName(text.CString());
  103. if (SDL_strlen(text.CString()) == 1)
  104. {
  105. if (text[0] >= 'A' && text[0] <= 'Z')
  106. {
  107. keyCode -= 32;
  108. }
  109. }
  110. keyEvent.character = (char16) keyCode;
  111. keyEvent.type = KEYEVENT_CHAR;
  112. return true;
  113. }
  114. }
  115. #endif