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