InputSourceKeyboard.cpp 3.5 KB

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