InputSourceKeyboard.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_F6:
  55. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  56. break;
  57. case SDLK_F7:
  58. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  59. break;
  60. case SDLK_F8:
  61. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  62. break;
  63. default:
  64. break;
  65. }
  66. return;
  67. }
  68. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  69. GH.events().dispatchShortcutPressed(shortcutsVector);
  70. }
  71. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  72. {
  73. if(key.repeat != 0)
  74. return; // ignore periodic event resends
  75. if (SDL_IsTextInputActive() == SDL_TRUE)
  76. {
  77. if (key.keysym.sym >= ' ' && key.keysym.sym < 0x80)
  78. return; // printable character - will be handled as text input
  79. }
  80. assert(key.state == SDL_RELEASED);
  81. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  82. GH.events().dispatchShortcutReleased(shortcutsVector);
  83. }
  84. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  85. {
  86. #ifdef VCMI_MAC
  87. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  88. #else
  89. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  90. #endif
  91. }
  92. bool InputSourceKeyboard::isKeyboardAltDown() const
  93. {
  94. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  95. }
  96. bool InputSourceKeyboard::isKeyboardShiftDown() const
  97. {
  98. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  99. }