InputSourceKeyboard.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/ShortcutHandler.h"
  17. #include "../CServerHandler.h"
  18. #include "../globalLobby/GlobalLobbyClient.h"
  19. #include <SDL_clipboard.h>
  20. #include <SDL_events.h>
  21. #include <SDL_hints.h>
  22. InputSourceKeyboard::InputSourceKeyboard()
  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. }
  29. void InputSourceKeyboard::handleEventKeyDown(const SDL_KeyboardEvent & key)
  30. {
  31. std::string keyName = SDL_GetKeyName(key.keysym.sym);
  32. logGlobal->trace("keyboard: key '%s' pressed", keyName);
  33. assert(key.state == SDL_PRESSED);
  34. if (SDL_IsTextInputActive() == SDL_TRUE)
  35. {
  36. if(key.keysym.sym == SDLK_v && isKeyboardCtrlDown())
  37. {
  38. char * clipboardBuffer = SDL_GetClipboardText();
  39. std::string clipboardContent = clipboardBuffer;
  40. boost::erase_all(clipboardContent, "\r");
  41. boost::erase_all(clipboardContent, "\n");
  42. GH.events().dispatchTextInput(clipboardContent);
  43. SDL_free(clipboardBuffer);
  44. return;
  45. }
  46. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  47. return; // printable character - will be handled as text input
  48. } else {
  49. if(key.repeat != 0)
  50. return; // ignore periodic event resends
  51. }
  52. if(key.keysym.sym == SDLK_TAB && isKeyboardCtrlDown())
  53. {
  54. CSH->getGlobalLobby().activateInterface();
  55. }
  56. if(key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  57. {
  58. //TODO: we need some central place for all interface-independent hotkeys
  59. Settings s = settings.write["session"];
  60. switch(key.keysym.sym)
  61. {
  62. case SDLK_F6:
  63. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  64. break;
  65. case SDLK_F7:
  66. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  67. break;
  68. case SDLK_F8:
  69. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  70. break;
  71. default:
  72. break;
  73. }
  74. return;
  75. }
  76. auto shortcutsVector = GH.shortcuts().translateKeycode(keyName);
  77. GH.events().dispatchShortcutPressed(shortcutsVector);
  78. }
  79. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  80. {
  81. if(key.repeat != 0)
  82. return; // ignore periodic event resends
  83. std::string keyName = SDL_GetKeyName(key.keysym.sym);
  84. logGlobal->trace("keyboard: key '%s' released", keyName);
  85. if (SDL_IsTextInputActive() == SDL_TRUE)
  86. {
  87. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  88. return; // printable character - will be handled as text input
  89. }
  90. assert(key.state == SDL_RELEASED);
  91. auto shortcutsVector = GH.shortcuts().translateKeycode(keyName);
  92. GH.events().dispatchShortcutReleased(shortcutsVector);
  93. }
  94. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  95. {
  96. #ifdef VCMI_MAC
  97. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  98. #else
  99. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  100. #endif
  101. }
  102. bool InputSourceKeyboard::isKeyboardAltDown() const
  103. {
  104. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  105. }
  106. bool InputSourceKeyboard::isKeyboardShiftDown() const
  107. {
  108. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  109. }