InputSourceKeyboard.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <SDL_clipboard.h>
  18. #include <SDL_events.h>
  19. #include <SDL_hints.h>
  20. InputSourceKeyboard::InputSourceKeyboard()
  21. {
  22. #ifdef VCMI_MAC
  23. // Ctrl+click should be treated as a right click on Mac OS X
  24. SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, "1");
  25. #endif
  26. }
  27. void InputSourceKeyboard::handleEventKeyDown(const SDL_KeyboardEvent & key)
  28. {
  29. if (SDL_IsTextInputActive() == SDL_TRUE)
  30. {
  31. if(key.keysym.sym == SDLK_v) {
  32. std::string clipboardContent = SDL_GetClipboardText();
  33. boost::erase_all(clipboardContent, "\r");
  34. boost::erase_all(clipboardContent, "\n");
  35. GH.events().dispatchTextInput(clipboardContent);
  36. return;
  37. }
  38. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  39. return; // printable character - will be handled as text input
  40. } else {
  41. if(key.repeat != 0)
  42. return; // ignore periodic event resends
  43. }
  44. assert(key.state == SDL_PRESSED);
  45. if(key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  46. {
  47. //TODO: we need some central place for all interface-independent hotkeys
  48. Settings s = settings.write["session"];
  49. switch(key.keysym.sym)
  50. {
  51. case SDLK_F5:
  52. if(settings["session"]["spectate-locked-pim"].Bool())
  53. CPlayerInterface::pim->unlock();
  54. else
  55. CPlayerInterface::pim->lock();
  56. s["spectate-locked-pim"].Bool() = !settings["session"]["spectate-locked-pim"].Bool();
  57. break;
  58. case SDLK_F6:
  59. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  60. break;
  61. case SDLK_F7:
  62. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  63. break;
  64. case SDLK_F8:
  65. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  66. break;
  67. default:
  68. break;
  69. }
  70. return;
  71. }
  72. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  73. GH.events().dispatchShortcutPressed(shortcutsVector);
  74. }
  75. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  76. {
  77. if(key.repeat != 0)
  78. return; // ignore periodic event resends
  79. if (SDL_IsTextInputActive() == SDL_TRUE)
  80. {
  81. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  82. return; // printable character - will be handled as text input
  83. }
  84. assert(key.state == SDL_RELEASED);
  85. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  86. GH.events().dispatchShortcutReleased(shortcutsVector);
  87. }
  88. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  89. {
  90. #ifdef VCMI_MAC
  91. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  92. #else
  93. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  94. #endif
  95. }
  96. bool InputSourceKeyboard::isKeyboardAltDown() const
  97. {
  98. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  99. }
  100. bool InputSourceKeyboard::isKeyboardShiftDown() const
  101. {
  102. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  103. }