InputSourceKeyboard.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_GetKeyName(key.keysym.sym), 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. if (vstd::contains(shortcutsVector, EShortcut::MAIN_MENU_LOBBY))
  78. ENGINE->user().onGlobalLobbyInterfaceActivated();
  79. if (vstd::contains(shortcutsVector, EShortcut::GLOBAL_FULLSCREEN))
  80. {
  81. Settings full = settings.write["video"]["fullscreen"];
  82. full->Bool() = !full->Bool();
  83. ENGINE->onScreenResize(true);
  84. }
  85. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_TRACK_HERO))
  86. {
  87. Settings s = settings.write["session"];
  88. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  89. }
  90. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE))
  91. {
  92. Settings s = settings.write["session"];
  93. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  94. }
  95. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE_RESULT))
  96. {
  97. Settings s = settings.write["session"];
  98. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  99. }
  100. ENGINE->events().dispatchShortcutPressed(shortcutsVector);
  101. }
  102. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  103. {
  104. if(key.repeat != 0)
  105. return; // ignore periodic event resends
  106. if(handleBackRightMouseButton && key.keysym.scancode == SDL_SCANCODE_AC_BACK) // on some android devices right mouse button is "back"
  107. {
  108. ENGINE->events().dispatchClosePopup(ENGINE->getCursorPosition());
  109. return;
  110. }
  111. std::string keyName = getKeyNameWithModifiers(SDL_GetKeyName(key.keysym.sym), true);
  112. logGlobal->trace("keyboard: key '%s' released", keyName);
  113. if (SDL_IsTextInputActive() == SDL_TRUE)
  114. {
  115. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  116. return; // printable character - will be handled as text input
  117. }
  118. assert(key.state == SDL_RELEASED);
  119. auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName);
  120. ENGINE->events().dispatchShortcutReleased(shortcutsVector);
  121. }
  122. bool InputSourceKeyboard::isKeyboardCmdDown() const
  123. {
  124. #ifdef VCMI_APPLE
  125. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  126. #else
  127. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  128. #endif
  129. }
  130. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  131. {
  132. #ifdef VCMI_APPLE
  133. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL] ||
  134. SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  135. #else
  136. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  137. #endif
  138. }
  139. bool InputSourceKeyboard::isKeyboardAltDown() const
  140. {
  141. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  142. }
  143. bool InputSourceKeyboard::isKeyboardShiftDown() const
  144. {
  145. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  146. }