From f213a7d54e3e9e67f8ad4dd5b3775e0c4469e81a Mon Sep 17 00:00:00 2001 From: Giulio Camuffo Date: Mon, 27 Jun 2016 11:17:57 +0200 Subject: [PATCH] Fix the key code of key events when control is pressed Change-Id: I51a57a32d8263e663a48dac15881d685359bc91d Reviewed-by: Jan Arne Petersen Reviewed-by: Pier Luigi Fiorini --- .../wayland/qwaylandinputcontext.cpp | 5 +-- .../platforms/wayland/qwaylandinputdevice.cpp | 3 +- .../platforms/wayland/shared/qwaylandxkb.cpp | 31 ++++++++----------- .../platforms/wayland/shared/qwaylandxkb_p.h | 5 +-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/plugins/platforms/wayland/qwaylandinputcontext.cpp b/src/plugins/platforms/wayland/qwaylandinputcontext.cpp index 509965d2f1a..5a58d6d7509 100644 --- a/src/plugins/platforms/wayland/qwaylandinputcontext.cpp +++ b/src/plugins/platforms/wayland/qwaylandinputcontext.cpp @@ -335,8 +335,9 @@ void QWaylandTextInput::zwp_text_input_v2_keysym(uint32_t time, uint32_t sym, ui Qt::KeyboardModifiers qtModifiers = modifiersToQtModifiers(modifiers); QEvent::Type type = QWaylandXkb::toQtEventType(state); - const QString &text = QWaylandXkb::textFromKeysym(sym, qtModifiers); - int qtkey = QWaylandXkb::keysymToQtKey(sym, qtModifiers, text); + QString text; + int qtkey; + std::tie(qtkey, text) = QWaylandXkb::keysymToQtKey(sym, qtModifiers); QWindowSystemInterface::handleKeyEvent(QGuiApplication::focusWindow(), time, type, qtkey, qtModifiers, text); diff --git a/src/plugins/platforms/wayland/qwaylandinputdevice.cpp b/src/plugins/platforms/wayland/qwaylandinputdevice.cpp index 613d517a3f0..13ab5efe6bf 100644 --- a/src/plugins/platforms/wayland/qwaylandinputdevice.cpp +++ b/src/plugins/platforms/wayland/qwaylandinputdevice.cpp @@ -707,8 +707,7 @@ void QWaylandInputDevice::Keyboard::keyboard_key(uint32_t serial, uint32_t time, Qt::KeyboardModifiers modifiers = mParent->modifiers(); - text = QWaylandXkb::textFromKeysym(sym, modifiers); - qtkey = QWaylandXkb::keysymToQtKey(sym, modifiers, text); + std::tie(qtkey, text) = QWaylandXkb::keysymToQtKey(sym, modifiers); sendKey(window->window(), time, type, qtkey, modifiers, code, sym, mNativeModifiers, text); #else diff --git a/src/plugins/platforms/wayland/shared/qwaylandxkb.cpp b/src/plugins/platforms/wayland/shared/qwaylandxkb.cpp index 499257009cf..2afdcce8a05 100644 --- a/src/plugins/platforms/wayland/shared/qwaylandxkb.cpp +++ b/src/plugins/platforms/wayland/shared/qwaylandxkb.cpp @@ -293,8 +293,13 @@ static xkb_keysym_t toKeysymFromTable(uint32_t key) return 0; } -int QWaylandXkb::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers &modifiers, const QString &text) +std::pair QWaylandXkb::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers &modifiers) { + QString text; + uint utf32 = xkb_keysym_to_utf32(keysym); + if (utf32) + text = QString::fromUcs4(&utf32, 1); + int code = 0; if (keysym >= XKB_KEY_F1 && keysym <= XKB_KEY_F35) { @@ -316,7 +321,13 @@ int QWaylandXkb::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers &modif code = lookupKeysym(keysym); } - return code; + // Map control + letter to proper text + if (utf32 >= 'A' && utf32 <= '~' && (modifiers & Qt::ControlModifier)) { + utf32 &= ~0x60; + text = QString::fromUcs4(&utf32, 1); + } + + return { code, text }; } Qt::KeyboardModifiers QWaylandXkb::modifiers(struct xkb_state *state) @@ -342,22 +353,6 @@ QEvent::Type QWaylandXkb::toQtEventType(uint32_t state) return state != 0 ? QEvent::KeyPress : QEvent::KeyRelease; } -QString QWaylandXkb::textFromKeysym(uint32_t keysym, Qt::KeyboardModifiers modifiers) -{ - uint utf32 = xkb_keysym_to_utf32(keysym); - - // Map control + letter to proper text - if (utf32 >= 'A' && utf32 <= '~' && (modifiers & Qt::ControlModifier)) { - utf32 &= ~0x60; - return QString::fromUcs4(&utf32, 1); - } - - if (utf32) - return QString::fromUcs4(&utf32, 1); - - return QString(); -} - QVector QWaylandXkb::toKeysym(QKeyEvent *event) { QVector keysyms; diff --git a/src/plugins/platforms/wayland/shared/qwaylandxkb_p.h b/src/plugins/platforms/wayland/shared/qwaylandxkb_p.h index 9b5c935a5b5..cdebf1b08b0 100644 --- a/src/plugins/platforms/wayland/shared/qwaylandxkb_p.h +++ b/src/plugins/platforms/wayland/shared/qwaylandxkb_p.h @@ -47,6 +47,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE class QKeyEvent; @@ -54,11 +56,10 @@ class QKeyEvent; class QWaylandXkb { public: - static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers &modifiers, const QString &text); + static std::pair keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers &modifiers); static Qt::KeyboardModifiers modifiers(struct xkb_state *state); static QEvent::Type toQtEventType(uint32_t state); - static QString textFromKeysym(uint32_t keysym, Qt::KeyboardModifiers modifiers); static QVector toKeysym(QKeyEvent *event); };