InputSourceKeyboard.cpp 5.5 KB

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