InputSourceKeyboard.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * InputSourceKeyboard.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "InputSourceKeyboard.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../gui/EventDispatcher.h"
  17. #include "../gui/Shortcut.h"
  18. #include "../gui/ShortcutHandler.h"
  19. #include "../CServerHandler.h"
  20. #include "../globalLobby/GlobalLobbyClient.h"
  21. #include <SDL_clipboard.h>
  22. #include <SDL_events.h>
  23. #include <SDL_hints.h>
  24. InputSourceKeyboard::InputSourceKeyboard()
  25. : handleBackRightMouseButton(settings["input"]["handleBackRightMouseButton"].Bool())
  26. {
  27. #ifdef VCMI_MAC
  28. // Ctrl+click should be treated as a right click on Mac OS X
  29. SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, "1");
  30. #endif
  31. SDL_SetHint(SDL_HINT_ANDROID_TRAP_BACK_BUTTON, handleBackRightMouseButton ? "1" : "0");
  32. }
  33. std::string InputSourceKeyboard::getKeyNameWithModifiers(const std::string & keyName, bool keyUp)
  34. {
  35. std::string result;
  36. if(!keyUp)
  37. {
  38. wasKeyboardCtrlDown = isKeyboardCtrlDown();
  39. wasKeyboardAltDown = isKeyboardAltDown();
  40. wasKeyboardShiftDown = isKeyboardShiftDown();
  41. }
  42. if (wasKeyboardCtrlDown)
  43. result += "Ctrl+";
  44. if (wasKeyboardAltDown)
  45. result += "Alt+";
  46. if (wasKeyboardShiftDown)
  47. result += "Shift+";
  48. result += keyName;
  49. return result;
  50. }
  51. void InputSourceKeyboard::handleEventKeyDown(const SDL_KeyboardEvent & key)
  52. {
  53. std::string keyName = getKeyNameWithModifiers(SDL_GetKeyName(key.keysym.sym), false);
  54. logGlobal->trace("keyboard: key '%s' pressed", keyName);
  55. assert(key.state == SDL_PRESSED);
  56. if (SDL_IsTextInputActive() == SDL_TRUE)
  57. {
  58. if(key.keysym.sym == SDLK_v && isKeyboardCtrlDown())
  59. {
  60. char * clipboardBuffer = SDL_GetClipboardText();
  61. std::string clipboardContent = clipboardBuffer;
  62. boost::erase_all(clipboardContent, "\r");
  63. boost::erase_all(clipboardContent, "\n");
  64. ENGINE->events().dispatchTextInput(clipboardContent);
  65. SDL_free(clipboardBuffer);
  66. return;
  67. }
  68. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  69. return; // printable character - will be handled as text input
  70. } else {
  71. if(key.repeat != 0)
  72. return; // ignore periodic event resends
  73. }
  74. if(handleBackRightMouseButton && key.keysym.scancode == SDL_SCANCODE_AC_BACK) // on some android devices right mouse button is "back"
  75. {
  76. ENGINE->events().dispatchShowPopup(ENGINE->getCursorPosition(), settings["input"]["mouseToleranceDistance"].Integer());
  77. return;
  78. }
  79. auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName);
  80. if (vstd::contains(shortcutsVector, EShortcut::MAIN_MENU_LOBBY))
  81. GAME->server().getGlobalLobby().activateInterface();
  82. if (vstd::contains(shortcutsVector, EShortcut::GLOBAL_FULLSCREEN))
  83. {
  84. Settings full = settings.write["video"]["fullscreen"];
  85. full->Bool() = !full->Bool();
  86. ENGINE->onScreenResize(true);
  87. }
  88. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_TRACK_HERO))
  89. {
  90. Settings s = settings.write["session"];
  91. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  92. }
  93. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE))
  94. {
  95. Settings s = settings.write["session"];
  96. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  97. }
  98. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE_RESULT))
  99. {
  100. Settings s = settings.write["session"];
  101. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  102. }
  103. ENGINE->events().dispatchShortcutPressed(shortcutsVector);
  104. }
  105. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  106. {
  107. if(key.repeat != 0)
  108. return; // ignore periodic event resends
  109. if(handleBackRightMouseButton && key.keysym.scancode == SDL_SCANCODE_AC_BACK) // on some android devices right mouse button is "back"
  110. {
  111. ENGINE->events().dispatchClosePopup(ENGINE->getCursorPosition());
  112. return;
  113. }
  114. std::string keyName = getKeyNameWithModifiers(SDL_GetKeyName(key.keysym.sym), true);
  115. logGlobal->trace("keyboard: key '%s' released", keyName);
  116. if (SDL_IsTextInputActive() == SDL_TRUE)
  117. {
  118. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  119. return; // printable character - will be handled as text input
  120. }
  121. assert(key.state == SDL_RELEASED);
  122. auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName);
  123. ENGINE->events().dispatchShortcutReleased(shortcutsVector);
  124. }
  125. bool InputSourceKeyboard::isKeyboardCmdDown() const
  126. {
  127. #ifdef VCMI_APPLE
  128. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  129. #else
  130. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  131. #endif
  132. }
  133. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  134. {
  135. #ifdef VCMI_APPLE
  136. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL] ||
  137. SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  138. #else
  139. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  140. #endif
  141. }
  142. bool InputSourceKeyboard::isKeyboardAltDown() const
  143. {
  144. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  145. }
  146. bool InputSourceKeyboard::isKeyboardShiftDown() const
  147. {
  148. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  149. }