InputSourceKeyboard.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) const
  31. {
  32. std::string result;
  33. if (isKeyboardCtrlDown())
  34. result += "Ctrl+";
  35. if (isKeyboardAltDown())
  36. result += "Alt+";
  37. if (isKeyboardShiftDown())
  38. result += "Shift+";
  39. result += keyName;
  40. return result;
  41. }
  42. void InputSourceKeyboard::handleEventKeyDown(const SDL_KeyboardEvent & key)
  43. {
  44. std::string keyName = getKeyNameWithModifiers(SDL_GetKeyName(key.keysym.sym));
  45. logGlobal->trace("keyboard: key '%s' pressed", keyName);
  46. assert(key.state == SDL_PRESSED);
  47. if (SDL_IsTextInputActive() == SDL_TRUE)
  48. {
  49. if(key.keysym.sym == SDLK_v && isKeyboardCtrlDown())
  50. {
  51. char * clipboardBuffer = SDL_GetClipboardText();
  52. std::string clipboardContent = clipboardBuffer;
  53. boost::erase_all(clipboardContent, "\r");
  54. boost::erase_all(clipboardContent, "\n");
  55. GH.events().dispatchTextInput(clipboardContent);
  56. SDL_free(clipboardBuffer);
  57. return;
  58. }
  59. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  60. return; // printable character - will be handled as text input
  61. } else {
  62. if(key.repeat != 0)
  63. return; // ignore periodic event resends
  64. }
  65. auto shortcutsVector = GH.shortcuts().translateKeycode(keyName);
  66. if (vstd::contains(shortcutsVector, EShortcut::MAIN_MENU_LOBBY))
  67. CSH->getGlobalLobby().activateInterface();
  68. if (vstd::contains(shortcutsVector, EShortcut::GLOBAL_FULLSCREEN))
  69. {
  70. Settings full = settings.write["video"]["fullscreen"];
  71. full->Bool() = !full->Bool();
  72. GH.onScreenResize(true);
  73. }
  74. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_TRACK_HERO))
  75. {
  76. Settings s = settings.write["session"];
  77. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  78. }
  79. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE))
  80. {
  81. Settings s = settings.write["session"];
  82. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  83. }
  84. if (vstd::contains(shortcutsVector, EShortcut::SPECTATE_SKIP_BATTLE_RESULT))
  85. {
  86. Settings s = settings.write["session"];
  87. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  88. }
  89. GH.events().dispatchShortcutPressed(shortcutsVector);
  90. }
  91. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  92. {
  93. if(key.repeat != 0)
  94. return; // ignore periodic event resends
  95. std::string keyName = getKeyNameWithModifiers(SDL_GetKeyName(key.keysym.sym));
  96. logGlobal->trace("keyboard: key '%s' released", keyName);
  97. if (SDL_IsTextInputActive() == SDL_TRUE)
  98. {
  99. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  100. return; // printable character - will be handled as text input
  101. }
  102. assert(key.state == SDL_RELEASED);
  103. auto shortcutsVector = GH.shortcuts().translateKeycode(keyName);
  104. GH.events().dispatchShortcutReleased(shortcutsVector);
  105. }
  106. bool InputSourceKeyboard::isKeyboardCmdDown() const
  107. {
  108. #ifdef VCMI_APPLE
  109. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  110. #else
  111. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  112. #endif
  113. }
  114. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  115. {
  116. #ifdef VCMI_APPLE
  117. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL] ||
  118. SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  119. #else
  120. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  121. #endif
  122. }
  123. bool InputSourceKeyboard::isKeyboardAltDown() const
  124. {
  125. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  126. }
  127. bool InputSourceKeyboard::isKeyboardShiftDown() const
  128. {
  129. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  130. }