WebKeyboardWindows.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_WINDOWS
  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_windows.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 SDLScanCodeToWindowsScanCode(SDL_Scancode code, LPARAM& lParam, WPARAM& wParam )
  35. {
  36. wParam = 0;
  37. lParam = 0;
  38. int numCodes = sizeof(windows_scancode_table)/sizeof(SDL_Scancode);
  39. int windowsScanCode = -1;
  40. for (int i = 0; i < numCodes; i++)
  41. {
  42. if (windows_scancode_table[i] == code)
  43. {
  44. windowsScanCode = i;
  45. break;
  46. }
  47. }
  48. if (windowsScanCode != -1)
  49. {
  50. wParam = MapVirtualKey(windowsScanCode, MAPVK_VSC_TO_VK);
  51. lParam = windowsScanCode << 16;
  52. }
  53. switch (code)
  54. {
  55. case SDL_SCANCODE_RIGHT:
  56. wParam = VK_RIGHT;
  57. break;
  58. case SDL_SCANCODE_LEFT:
  59. wParam = VK_LEFT;
  60. break;
  61. case SDL_SCANCODE_UP:
  62. wParam = VK_UP;
  63. break;
  64. case SDL_SCANCODE_DOWN:
  65. wParam = VK_DOWN;
  66. break;
  67. case SDL_SCANCODE_DELETE:
  68. wParam = VK_DELETE;
  69. break;
  70. case SDL_SCANCODE_BACKSPACE:
  71. wParam = VK_BACK;
  72. break;
  73. }
  74. return wParam != 0 || lParam != 0;
  75. }
  76. bool ConvertKeyEvent(Input* input, const StringHash eventType, VariantMap& eventData, CefKeyEvent& keyEvent)
  77. {
  78. if (eventType != "KeyDown" && eventType != "KeyUp")
  79. {
  80. ATOMIC_LOGERROR("ConvertKeyEvent - Unknown event type");
  81. return false;
  82. }
  83. WebKeyEvent wk(eventType, eventData);
  84. if (wk.scanCode == SDL_SCANCODE_RETURN)
  85. {
  86. if (!wk.keyDown)
  87. return false;
  88. keyEvent.windows_key_code = VK_RETURN;
  89. keyEvent.native_key_code = (int) 0;
  90. keyEvent.type = KEYEVENT_RAWKEYDOWN;
  91. return true;
  92. }
  93. LPARAM lParam;
  94. WPARAM wParam;
  95. keyEvent.modifiers = EVENTFLAG_NONE;
  96. if (wk.qual & QUAL_SHIFT)
  97. keyEvent.modifiers |= EVENTFLAG_SHIFT_DOWN;
  98. if (wk.qual & QUAL_ALT)
  99. keyEvent.modifiers |= EVENTFLAG_ALT_DOWN;
  100. if (wk.qual & QUAL_CTRL)
  101. keyEvent.modifiers |= EVENTFLAG_CONTROL_DOWN;
  102. if (SDLScanCodeToWindowsScanCode((SDL_Scancode) wk.scanCode, lParam, wParam))
  103. {
  104. keyEvent.windows_key_code = (int) wParam;
  105. keyEvent.native_key_code = (int) lParam;
  106. keyEvent.type = wk.keyDown ? KEYEVENT_RAWKEYDOWN : KEYEVENT_KEYUP;
  107. }
  108. return true;
  109. }
  110. bool ConvertTextInputEvent(StringHash eventType, VariantMap& eventData, CefKeyEvent& keyEvent)
  111. {
  112. if (eventType != "TextInput")
  113. {
  114. ATOMIC_LOGERROR("ConvertTextInputEvent - Unknown event type");
  115. return false;
  116. }
  117. String text = eventData[TextInput::P_TEXT].GetString();
  118. SDL_Keycode keyCode = SDL_GetKeyFromName(text.CString());
  119. if (SDL_strlen(text.CString()) == 1)
  120. {
  121. if (text[0] >= 'A' && text[0] <= 'Z')
  122. {
  123. keyCode -= 32;
  124. }
  125. }
  126. keyEvent.windows_key_code = (int) keyCode;
  127. keyEvent.native_key_code = 0;// (int) lParam;
  128. keyEvent.type = KEYEVENT_CHAR;
  129. return true;
  130. }
  131. }
  132. #endif