From 937d7b9c264d78433fc47832b66cd62640393b54 Mon Sep 17 00:00:00 2001 From: Zeno Albisser Date: Wed, 19 Nov 2014 23:07:35 +0100 Subject: Add Keyboard modifiers to MotionEventQt. Also fix the mapping of Ctrl and Meta keys on Mac OSX. On this platform Qt automatically maps Meta to Control and vice versa. Chromium however does not expect such behavior. Change-Id: Ia9913322ae8ae6ffd99feb6edfc91b9ea752c5cb Reviewed-by: Pierre Rossi --- src/core/render_widget_host_view_qt.cpp | 48 ++++++++- src/core/web_event_factory.cpp | 50 ++++++++- .../qmltests/data/keyboardModifierMapping.html | 36 +++++++ .../qmltests/data/tst_keyboardModifierMapping.qml | 119 +++++++++++++++++++++ tests/auto/quick/qmltests/qmltests.pro | 6 +- 5 files changed, 250 insertions(+), 9 deletions(-) create mode 100644 tests/auto/quick/qmltests/data/keyboardModifierMapping.html create mode 100644 tests/auto/quick/qmltests/data/tst_keyboardModifierMapping.qml diff --git a/src/core/render_widget_host_view_qt.cpp b/src/core/render_widget_host_view_qt.cpp index 9555c85a5..e17459634 100644 --- a/src/core/render_widget_host_view_qt.cpp +++ b/src/core/render_widget_host_view_qt.cpp @@ -152,12 +152,37 @@ static inline bool compareTouchPoints(const QTouchEvent::TouchPoint &lhs, const return lhs.state() < rhs.state(); } +static inline int flagsFromModifiers(Qt::KeyboardModifiers modifiers) +{ + int modifierFlags = ui::EF_NONE; +#if defined(Q_OS_OSX) + if (!qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta)) { + if ((modifiers & Qt::ControlModifier) != 0) + modifierFlags |= ui::EF_COMMAND_DOWN; + if ((modifiers & Qt::MetaModifier) != 0) + modifierFlags |= ui::EF_CONTROL_DOWN; + } else +#endif + { + if ((modifiers & Qt::ControlModifier) != 0) + modifierFlags |= ui::EF_CONTROL_DOWN; + if ((modifiers & Qt::MetaModifier) != 0) + modifierFlags |= ui::EF_COMMAND_DOWN; + } + if ((modifiers & Qt::ShiftModifier) != 0) + modifierFlags |= ui::EF_SHIFT_DOWN; + if ((modifiers & Qt::AltModifier) != 0) + modifierFlags |= ui::EF_ALT_DOWN; + return modifierFlags; +} + class MotionEventQt : public ui::MotionEvent { public: - MotionEventQt(const QList &touchPoints, const base::TimeTicks &eventTime, Action action, int index = -1) + MotionEventQt(const QList &touchPoints, const base::TimeTicks &eventTime, Action action, const Qt::KeyboardModifiers modifiers, int index = -1) : touchPoints(touchPoints) , eventTime(eventTime) , action(action) + , flags(flagsFromModifiers(modifiers)) , index(index) { // ACTION_DOWN and ACTION_UP must be accesssed through pointer_index 0 @@ -173,7 +198,21 @@ public: virtual float GetY(size_t pointer_index) const Q_DECL_OVERRIDE { return touchPoints.at(pointer_index).pos().y(); } virtual float GetRawX(size_t pointer_index) const Q_DECL_OVERRIDE { return touchPoints.at(pointer_index).screenPos().x(); } virtual float GetRawY(size_t pointer_index) const Q_DECL_OVERRIDE { return touchPoints.at(pointer_index).screenPos().y(); } - virtual float GetTouchMajor(size_t pointer_index) const Q_DECL_OVERRIDE { return touchPoints.at(pointer_index).rect().height(); } + virtual float GetTouchMajor(size_t pointer_index) const Q_DECL_OVERRIDE + { + QRectF touchRect = touchPoints.at(pointer_index).rect(); + return std::max(touchRect.height(), touchRect.width()); + } + virtual float GetTouchMinor(size_t pointer_index) const Q_DECL_OVERRIDE + { + QRectF touchRect = touchPoints.at(pointer_index).rect(); + return std::min(touchRect.height(), touchRect.width()); + } + virtual float GetOrientation(size_t pointer_index) const Q_DECL_OVERRIDE + { + return 0; + } + virtual int GetFlags() const Q_DECL_OVERRIDE { return flags; } virtual float GetPressure(size_t pointer_index) const Q_DECL_OVERRIDE { return touchPoints.at(pointer_index).pressure(); } virtual base::TimeTicks GetEventTime() const Q_DECL_OVERRIDE { return eventTime; } @@ -189,6 +228,7 @@ private: QList touchPoints; base::TimeTicks eventTime; Action action; + int flags; int index; }; @@ -929,7 +969,7 @@ void RenderWidgetHostViewQt::handleTouchEvent(QTouchEvent *ev) QList touchPoints = mapTouchPointIds(ev->touchPoints()); if (ev->type() == QEvent::TouchCancel) { - MotionEventQt cancelEvent(touchPoints, eventTimestamp, ui::MotionEvent::ACTION_CANCEL); + MotionEventQt cancelEvent(touchPoints, eventTimestamp, ui::MotionEvent::ACTION_CANCEL, ev->modifiers()); processMotionEvent(cancelEvent); return; } @@ -962,7 +1002,7 @@ void RenderWidgetHostViewQt::handleTouchEvent(QTouchEvent *ev) continue; } - MotionEventQt motionEvent(touchPoints, eventTimestamp, action, i); + MotionEventQt motionEvent(touchPoints, eventTimestamp, action, ev->modifiers(), i); processMotionEvent(motionEvent); } } diff --git a/src/core/web_event_factory.cpp b/src/core/web_event_factory.cpp index 36e96121c..8dfb2c574 100644 --- a/src/core/web_event_factory.cpp +++ b/src/core/web_event_factory.cpp @@ -64,6 +64,7 @@ #include "web_event_factory.h" #include "third_party/WebKit/Source/platform/WindowsKeyboardCodes.h" +#include #include #include #include @@ -478,14 +479,52 @@ static unsigned mouseButtonsModifiersForEvent(const T* event) return ret; } +// If only a modifier key is pressed, Qt only reports the key code. +// But Chromium also expects the modifier being set. +static inline WebInputEvent::Modifiers modifierForKeyCode(int key) +{ + switch (key) { + case Qt::Key_Shift: + return WebInputEvent::ShiftKey; + case Qt::Key_Alt: + return WebInputEvent::AltKey; +#if defined(Q_OS_OSX) + case Qt::Key_Control: + return (!qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta)) ? WebInputEvent::MetaKey : WebInputEvent::ControlKey; + case Qt::Key_Meta: + return (!qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta)) ? WebInputEvent::ControlKey : WebInputEvent::MetaKey; +#else + case Qt::Key_Control: + return WebInputEvent::ControlKey; + case Qt::Key_Meta: + return WebInputEvent::MetaKey; +#endif + default: + return static_cast(0); + } +} + static inline WebInputEvent::Modifiers modifiersForEvent(const QInputEvent* event) { unsigned result = 0; Qt::KeyboardModifiers modifiers = event->modifiers(); +#if defined(Q_OS_OSX) + if (!qApp->testAttribute(Qt::AA_MacDontSwapCtrlAndMeta)) { + if (modifiers & Qt::ControlModifier) + result |= WebInputEvent::MetaKey; + if (modifiers & Qt::MetaModifier) + result |= WebInputEvent::ControlKey; + } else +#endif + { + if (modifiers & Qt::ControlModifier) + result |= WebInputEvent::ControlKey; + if (modifiers & Qt::MetaModifier) + result |= WebInputEvent::MetaKey; + } + if (modifiers & Qt::ShiftModifier) result |= WebInputEvent::ShiftKey; - if (modifiers & Qt::ControlModifier) - result |= WebInputEvent::ControlKey; if (modifiers & Qt::AltModifier) result |= WebInputEvent::AltKey; if (modifiers & Qt::MetaModifier) @@ -503,9 +542,12 @@ static inline WebInputEvent::Modifiers modifiersForEvent(const QInputEvent* even result |= mouseButtonsModifiersForEvent(static_cast(event)); break; case QEvent::KeyPress: - case QEvent::KeyRelease: - if (static_cast(event)->isAutoRepeat()) + case QEvent::KeyRelease: { + const QKeyEvent *keyEvent = static_cast(event); + if (keyEvent->isAutoRepeat()) result |= WebInputEvent::IsAutoRepeat; + result |= modifierForKeyCode(keyEvent->key()); + } default: break; } diff --git a/tests/auto/quick/qmltests/data/keyboardModifierMapping.html b/tests/auto/quick/qmltests/data/keyboardModifierMapping.html new file mode 100644 index 000000000..b6d291207 --- /dev/null +++ b/tests/auto/quick/qmltests/data/keyboardModifierMapping.html @@ -0,0 +1,36 @@ + + +Alt is no
+Ctrl is no
+Meta is no
+last keycode: none
+ + + + + diff --git a/tests/auto/quick/qmltests/data/tst_keyboardModifierMapping.qml b/tests/auto/quick/qmltests/data/tst_keyboardModifierMapping.qml new file mode 100644 index 000000000..ddf025281 --- /dev/null +++ b/tests/auto/quick/qmltests/data/tst_keyboardModifierMapping.qml @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Quick Controls module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtTest 1.0 +import QtWebEngine 1.0 + +TestWebEngineView { + id: webEngineView + width: 400 + height: 300 + + SignalSpy { + id: spy + target: webEngineView + signalName: "titleChanged" + } + + TestCase { + name: "WebEngineViewKeyboardModifierMapping" + + when: false + Timer { + running: parent.windowShown + repeat: false + interval: 1 + onTriggered: parent.when = true + } + + function test_keyboardModifierMapping() { + webEngineView.url = Qt.resolvedUrl("keyboardModifierMapping.html") + waitForLoadSucceeded(); + var callbackCalled = false; + + // Alt + keyPress(Qt.Key_Alt); + runJavaScript("getPressedModifiers()", function(result) { + compare(result, "alt:pressed ctrl:no meta:no"); + callbackCalled = true; + }); + wait(100); + verify(callbackCalled); + keyRelease(Qt.Key_Alt) + callbackCalled = false; + + // Ctrl + // On mac Qt automatically translates Meta to Ctrl and vice versa. + // However, if sending the events manually no mapping is being done, + // so we have to do this here manually. + // For testing we assume that the flag Qt::AA_MacDontSwapCtrlAndMeta is NOT set. + keyPress(Qt.platform.os == "osx" ? Qt.Key_Meta : Qt.Key_Control); + runJavaScript("getPressedModifiers()", function(result) { + compare(result, "alt:released ctrl:pressed meta:no"); + callbackCalled = true; + }); + wait(100); + verify(callbackCalled); + keyRelease(Qt.platform.os == "osx" ? Qt.Key_Meta : Qt.Key_Control); + callbackCalled = false; + + // Meta (Command on Mac) + keyPress(Qt.platform.os == "osx" ? Qt.Key_Control : Qt.Key_Meta); + runJavaScript("getPressedModifiers()", function(result) { + compare(result, "alt:released ctrl:released meta:pressed"); + callbackCalled = true; + }); + wait(100); + verify(callbackCalled); + keyRelease(Qt.platform.os == "osx" ? Qt.Key_Control : Qt.Key_Meta); + callbackCalled = false; + + runJavaScript("getPressedModifiers()", function(result) { + compare(result, "alt:released ctrl:released meta:released"); + callbackCalled = true; + }); + wait(100); + verify(callbackCalled); + callbackCalled = false; + } + } +} diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro index 33a864cf1..6789b714b 100644 --- a/tests/auto/quick/qmltests/qmltests.pro +++ b/tests/auto/quick/qmltests/qmltests.pro @@ -16,6 +16,8 @@ OTHER_FILES += \ $$PWD/data/test1.html \ $$PWD/data/test2.html \ $$PWD/data/test3.html \ + $$PWD/data/test4.html \ + $$PWD/data/keyboardModifierMapping.html \ $$PWD/data/tst_desktopBehaviorLoadHtml.qml \ $$PWD/data/tst_favIconLoad.qml \ $$PWD/data/tst_linkHovered.qml \ @@ -29,7 +31,9 @@ OTHER_FILES += \ $$PWD/data/tst_navigationRequested.qml \ $$PWD/data/tst_properties.qml \ $$PWD/data/tst_runJavaScript.qml \ - $$PWD/data/tst_titleChanged.qml + $$PWD/data/tst_titleChanged.qml \ + $$PWD/data/tst_keyboardModifierMapping.qml \ + load(qt_build_paths) DEFINES += QUICK_TEST_SOURCE_DIR=\"\\\"$$PWD$${QMAKE_DIR_SEP}data\\\"\" -- cgit v1.2.3