InputSourceKeyboard.cpp 5.7 KB

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