EventFilter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <QQuickItem>
  10. #include <QKeyEvent>
  11. #include <QObject>
  12. static QStringList desktopWhiteListedKeys = { "Media Play",
  13. "Media Pause",
  14. "Media Stop",
  15. "Media Next",
  16. "Media Previous",
  17. "Media Rewind",
  18. "Media FastForward",
  19. "Back"};
  20. // These just happen to be mostly the same.
  21. static QStringList win32BlackListedKeys = { "Media Play",
  22. "Media Pause",
  23. "Media Stop",
  24. "Media Next",
  25. "Media Previous",
  26. "Media Rewind",
  27. "Media FastForward"};
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. static QString keyEventToKeyString(QKeyEvent *kevent)
  30. {
  31. // We ignore the KeypadModifier here since it's practically useless
  32. QKeySequence modifiers(kevent->modifiers() &= ~Qt::KeypadModifier);
  33. QKeySequence keySeq(kevent->key());
  34. QString key = keySeq.toString();
  35. // Qt tends to make up something weird for keys which don't cleanly map to text.
  36. // See e.g. QKeySequencePrivate::keyName() in the Qt sources.
  37. // We can't really know for sure which names are "good" or "bad", so we simply
  38. // allow printable latin1 characters, and mangle everything else.
  39. if ((key.size() > 0 && (key[0].unicode() < 32 || key[0].unicode() > 255)))
  40. {
  41. if (kevent->nativeVirtualKey() != 0)
  42. {
  43. key = "0x" + QString::number(kevent->nativeVirtualKey(), 16) + "V";
  44. }
  45. else
  46. {
  47. QString properKey;
  48. for (int n = 0; n < key.size(); n++)
  49. properKey += QString(n > 0 ? "+" : "") + "0x" + QString::number(key[n].unicode(), 16) + "Q";
  50. key = properKey;
  51. }
  52. }
  53. return modifiers.toString() + key;
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. bool EventFilter::eventFilter(QObject* watched, QEvent* event)
  57. {
  58. KonvergoWindow* window = qobject_cast<KonvergoWindow*>(parent());
  59. if (window && window->property("webDesktopMode").toBool())
  60. {
  61. // For desktop mode we don't want fullblown keyboard handling in
  62. // the host yet. We just want to handle some specific keyboard
  63. // events.
  64. //
  65. if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease || event->type() == QEvent::ShortcutOverride)
  66. {
  67. QKeyEvent* key = dynamic_cast<QKeyEvent*>(event);
  68. if (key)
  69. {
  70. InputBase::InputkeyState keystatus;
  71. if (event->type() == QEvent::KeyPress)
  72. keystatus = InputBase::KeyDown;
  73. else
  74. keystatus = InputBase::KeyUp;
  75. QString seq = keyEventToKeyString(key);
  76. #ifdef Q_OS_WIN32
  77. if (win32BlackListedKeys.contains(seq))
  78. {
  79. return true;
  80. }
  81. #endif
  82. if (desktopWhiteListedKeys.contains(seq))
  83. {
  84. InputKeyboard::Get().keyPress(seq, keystatus);
  85. return true;
  86. }
  87. }
  88. }
  89. if (event->type() == QEvent::MouseButtonPress)
  90. {
  91. QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event);
  92. if (mouseEvent) {
  93. QQuickItem* webView = window->findChild<QQuickItem*>("web");
  94. if (mouseEvent->button() == Qt::BackButton)
  95. QMetaObject::invokeMethod(webView, "goBack");
  96. if (mouseEvent->button() == Qt::ForwardButton)
  97. QMetaObject::invokeMethod(webView, "goForward");
  98. }
  99. }
  100. return QObject::eventFilter(watched, event);
  101. }
  102. SystemComponent& system = SystemComponent::Get();
  103. // ignore mouse events if mouse is disabled
  104. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "disablemouse").toBool() &&
  105. ((event->type() == QEvent::MouseMove) ||
  106. (event->type() == QEvent::MouseButtonPress) ||
  107. (event->type() == QEvent::MouseButtonRelease) ||
  108. (event->type() == QEvent::MouseButtonDblClick)))
  109. {
  110. return true;
  111. }
  112. if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
  113. {
  114. // In konvergo we intercept all keyboard events and translate them
  115. // into web client actions. We need to do this so that we can remap
  116. // keyboard buttons to different events.
  117. //
  118. InputBase::InputkeyState keystatus;
  119. if (event->type() == QEvent::KeyPress)
  120. keystatus = InputBase::KeyDown;
  121. else
  122. keystatus = InputBase::KeyUp;
  123. QKeyEvent* kevent = dynamic_cast<QKeyEvent*>(event);
  124. if (!kevent)
  125. return QObject::eventFilter(watched, event);
  126. QString keyName = keyEventToKeyString(kevent);
  127. #ifdef Q_OS_WIN32
  128. // On Windows, ignore media keys as they are handled elsewhere.
  129. if (win32BlackListedKeys.contains(keyName))
  130. return true;
  131. #endif
  132. if (keystatus == InputBase::KeyDown)
  133. {
  134. // Swallow auto-repeated keys (isAutoRepeat doesn't always work - QTBUG-57335)
  135. // Do this only for non-modifier keys (QKeyEvent::text serves as a heuristic
  136. // to distinguish them from normal key presses)
  137. if (kevent->text().size())
  138. {
  139. if (m_currentKeyDown)
  140. return true;
  141. m_currentKeyDown = true;
  142. }
  143. }
  144. else
  145. m_currentKeyDown = false;
  146. system.setCursorVisibility(false);
  147. if (kevent->spontaneous() && !kevent->isAutoRepeat())
  148. {
  149. InputKeyboard::Get().keyPress(keyName, keystatus);
  150. return true;
  151. }
  152. }
  153. else if (event->type() == QEvent::MouseMove)
  154. {
  155. system.setCursorVisibility(true);
  156. }
  157. else if (event->type() == QEvent::Wheel)
  158. {
  159. return true;
  160. }
  161. else if (event->type() == QEvent::MouseButtonPress)
  162. {
  163. // ignore right clicks that would show context menu
  164. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
  165. if ((mouseEvent) && (mouseEvent->button() == Qt::RightButton))
  166. return true;
  167. }
  168. else if (event->type() == QEvent::Drop)
  169. {
  170. // QtWebEngine would accept the drop and unload web-client.
  171. return true;
  172. }
  173. return QObject::eventFilter(watched, event);
  174. }