InputSourceKeyboard.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }