InputSourceKeyboard.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. assert(key.state == SDL_PRESSED);
  31. if(key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  32. {
  33. //TODO: we need some central place for all interface-independent hotkeys
  34. Settings s = settings.write["session"];
  35. switch(key.keysym.sym)
  36. {
  37. case SDLK_F5:
  38. if(settings["session"]["spectate-locked-pim"].Bool())
  39. CPlayerInterface::pim->unlock();
  40. else
  41. CPlayerInterface::pim->lock();
  42. s["spectate-locked-pim"].Bool() = !settings["session"]["spectate-locked-pim"].Bool();
  43. break;
  44. case SDLK_F6:
  45. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  46. break;
  47. case SDLK_F7:
  48. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  49. break;
  50. case SDLK_F8:
  51. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  52. break;
  53. default:
  54. break;
  55. }
  56. return;
  57. }
  58. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  59. GH.events().dispatchShortcutPressed(shortcutsVector);
  60. }
  61. void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
  62. {
  63. if(key.repeat != 0)
  64. return; // ignore periodic event resends
  65. assert(key.state == SDL_RELEASED);
  66. auto shortcutsVector = GH.shortcuts().translateKeycode(key.keysym.sym);
  67. GH.events().dispatchShortcutReleased(shortcutsVector);
  68. }
  69. bool InputSourceKeyboard::isKeyboardCtrlDown() const
  70. {
  71. #ifdef VCMI_MAC
  72. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  73. #else
  74. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  75. #endif
  76. }
  77. bool InputSourceKeyboard::isKeyboardAltDown() const
  78. {
  79. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  80. }
  81. bool InputSourceKeyboard::isKeyboardShiftDown() const
  82. {
  83. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  84. }