InputSourceKeyboard.cpp 4.8 KB

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