EventFilter.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Created by Tobias Hieta on 07/03/16.
  3. //
  4. #include "EventFilter.h"
  5. #include "system/SystemComponent.h"
  6. #include "settings/SettingsComponent.h"
  7. #include "input/InputKeyboard.h"
  8. #include "KonvergoWindow.h"
  9. #include <QKeyEvent>
  10. #include <QObject>
  11. static QStringList desktopWhiteListedKeys = { "Media Play",
  12. "Media Pause",
  13. "Media Stop",
  14. "Media Next",
  15. "Media Previous",
  16. "Media Rewind",
  17. "Media FastForward" };
  18. ///////////////////////////////////////////////////////////////////////////////////////////////////
  19. bool EventFilter::eventFilter(QObject* watched, QEvent* event)
  20. {
  21. KonvergoWindow* window = qobject_cast<KonvergoWindow*>(parent());
  22. if (window && window->property("webDesktopMode").toBool())
  23. {
  24. // For desktop mode we don't want fullblown keyboard handling in
  25. // the host yet. We just want to handle some specific keyboard
  26. // events.
  27. //
  28. if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease || event->type() == QEvent::ShortcutOverride)
  29. {
  30. QKeyEvent* key = dynamic_cast<QKeyEvent*>(event);
  31. if (key)
  32. {
  33. InputBase::InputkeyState keystatus;
  34. if (event->type() == QEvent::KeyPress)
  35. keystatus = InputBase::KeyDown;
  36. else
  37. keystatus = InputBase::KeyUp;
  38. QKeySequence seq(key->key() | (key->modifiers() &= ~Qt::KeypadModifier));
  39. if (desktopWhiteListedKeys.contains(seq.toString()))
  40. {
  41. InputKeyboard::Get().keyPress(seq, keystatus);
  42. return true;
  43. }
  44. }
  45. }
  46. return QObject::eventFilter(watched, event);
  47. }
  48. SystemComponent& system = SystemComponent::Get();
  49. // ignore mouse events if mouse is disabled
  50. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "disablemouse").toBool() &&
  51. ((event->type() == QEvent::MouseMove) ||
  52. (event->type() == QEvent::MouseButtonPress) ||
  53. (event->type() == QEvent::MouseButtonRelease) ||
  54. (event->type() == QEvent::MouseButtonDblClick)))
  55. {
  56. return true;
  57. }
  58. if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
  59. {
  60. // In konvergo we intercept all keyboard events and translate them
  61. // into web client actions. We need to do this so that we can remap
  62. // keyboard buttons to different events.
  63. //
  64. InputBase::InputkeyState keystatus;
  65. if (event->type() == QEvent::KeyPress)
  66. keystatus = InputBase::KeyDown;
  67. else
  68. keystatus = InputBase::KeyUp;
  69. if (keystatus == InputBase::KeyDown)
  70. {
  71. // Swallow auto-repeated keys (isAutoRepeat doesn't always work - QTBUG-57335)
  72. if (m_currentKeyDown)
  73. return true;
  74. m_currentKeyDown = true;
  75. }
  76. else
  77. m_currentKeyDown = false;
  78. QKeyEvent* kevent = dynamic_cast<QKeyEvent*>(event);
  79. if (kevent)
  80. {
  81. system.setCursorVisibility(false);
  82. if (kevent->spontaneous() && !kevent->isAutoRepeat())
  83. {
  84. // We ignore the KeypadModifier here since it's practically useless
  85. QKeySequence key(kevent->key() | (kevent->modifiers() &= ~Qt::KeypadModifier));
  86. InputKeyboard::Get().keyPress(key, keystatus);
  87. return true;
  88. }
  89. }
  90. }
  91. else if (event->type() == QEvent::MouseMove)
  92. {
  93. system.setCursorVisibility(true);
  94. }
  95. else if (event->type() == QEvent::Wheel)
  96. {
  97. return true;
  98. }
  99. else if (event->type() == QEvent::MouseButtonPress)
  100. {
  101. // ignore right clicks that would show context menu
  102. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
  103. if ((mouseEvent) && (mouseEvent->button() == Qt::RightButton))
  104. return true;
  105. }
  106. return QObject::eventFilter(watched, event);
  107. }