InputSourceKeyboard.cpp 3.2 KB

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