Skip to content

Instantly share code, notes, and snippets.

@sh-zam
Created November 20, 2021 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh-zam/53ccb480dc4e97f54d159ec3990c7081 to your computer and use it in GitHub Desktop.
Save sh-zam/53ccb480dc4e97f54d159ec3990c7081 to your computer and use it in GitHub Desktop.
diff --git a/src/plugins/platforms/android/androidjniinput.cpp b/src/plugins/platforms/android/androidjniinput.cpp
index 257d013fa8..f1ecf7318f 100644
--- a/src/plugins/platforms/android/androidjniinput.cpp
+++ b/src/plugins/platforms/android/androidjniinput.cpp
@@ -353,6 +353,8 @@ namespace QtAndroidInput
jint modifiers)
{
#if QT_CONFIG(tabletevent)
+ static const bool s_emulateTabletButtonPressAsKeyPress = qgetenv("QT_ANDROID_SYNTHESIZED_TABLET_KEYPRESS").toInt();
+
QPointF globalPosF(x, y);
QPoint globalPos((int)x, (int)y);
QWindow *tlw = m_mouseGrabber.data();
@@ -366,21 +368,6 @@ namespace QtAndroidInput
localPos = platformWindow ? platformWindow->mapFromGlobal(globalPos) : globalPosF;
}
- // Galaxy Note with plain Android:
- // 0 1 0 stylus press
- // 2 1 0 stylus drag
- // 1 1 0 stylus release
- // 0 1 2 stylus press with side-button held
- // 2 1 2 stylus drag with side-button held
- // 1 1 2 stylus release with side-button held
- // Galaxy Note 4 with Samsung firmware:
- // 0 1 0 stylus press
- // 2 1 0 stylus drag
- // 1 1 0 stylus release
- // 211 1 2 stylus press with side-button held
- // 213 1 2 stylus drag with side-button held
- // 212 1 2 stylus release with side-button held
- // when action == ACTION_UP (1) it's a release; otherwise we say which button is pressed
Qt::MouseButtons buttons = Qt::NoButton;
switch (action) {
case AMOTION_EVENT_ACTION_UP:
@@ -394,22 +381,40 @@ namespace QtAndroidInput
if (!buttonState)
buttons = Qt::LeftButton;
default:
- if (buttonState == AMOTION_EVENT_BUTTON_STYLUS_PRIMARY)
- buttons = Qt::MiddleButton;
- else if (buttonState == AMOTION_EVENT_BUTTON_STYLUS_SECONDARY)
- buttons = Qt::RightButton;
- else if (buttonState)
- buttons = Qt::MouseButtons(buttonState);
+ // if we're emulating key presses, we shouldn't send tablet button state
+ if (!s_emulateTabletButtonPressAsKeyPress) {
+ if (buttonState == AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) {
+ buttons = Qt::MiddleButton;
+ } else if (buttonState == AMOTION_EVENT_BUTTON_STYLUS_SECONDARY) {
+ buttons = Qt::RightButton;
+ } else if (buttonState) {
+ buttons = Qt::MouseButtons(buttonState);
+ }
+ }
break;
}
#ifdef QT_DEBUG_ANDROID_STYLUS
qDebug() << action << pointerType << buttonState << '@' << x << y << "pressure" << pressure << ": buttons" << buttons;
#endif
+ static bool buttonPressed = false;
+ if (s_emulateTabletButtonPressAsKeyPress) {
+ if (buttonState && !buttonPressed) {
+ QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyPress, Qt::Key_E, 0,
+ QLatin1Literal(), false);
+ QWindowSystemInterface::handleKeyEvent(0, QEvent::KeyRelease, Qt::Key_E,
+ 0, QLatin1Literal(), false);
+ buttonPressed = true;
+ } else if (!buttonState) {
+ // we release the button
+ buttonPressed = false;
+ }
+ }
+ QWindowSystemInterface::handleTabletEvent(
+ tlw, ulong(time), localPos, globalPosF, QTabletEvent::Stylus, pointerType,
+ buttons, pressure, tiltX, tiltY, 0., rotation, 0, deviceId,
+ mapAndroidModifiers(modifiers));
- QWindowSystemInterface::handleTabletEvent(tlw, ulong(time),
- localPos, globalPosF, QTabletEvent::Stylus, pointerType,
- buttons, pressure, tiltX, tiltY, 0., rotation, 0, deviceId, mapAndroidModifiers(modifiers));
#endif // QT_CONFIG(tabletevent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment