InputSourceKeyboard.cpp 5.4 KB

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