remotekeyboard: Move workaround to the c++ side and make it more generic

The QKeyEvent::text we get from a Ctrl+W press is a weird unicode
charcater for ^W (see comment). We don't deal with that thing, create
the controlled letter using QKeySequence and move on.
This commit is contained in:
Aleix Pol 2023-05-16 22:17:54 +02:00 committed by Aleix Pol Gonzalez
parent 6897b1863b
commit b61ba29673
2 changed files with 17 additions and 16 deletions

View file

@ -79,20 +79,8 @@ TextField {
function sendEvent(event) { function sendEvent(event) {
if (remoteKeyboard) { if (remoteKeyboard) {
var transEvent = JSON.parse(JSON.stringify(event)); // transform to anonymous object var transEvent = JSON.parse(JSON.stringify(event)); // transform to anonymous object
if (transEvent.modifiers & Qt.ControlModifier) {
// special handling for ctrl+c/v/x/a, for which only 'key' gets
// set, but no visible 'text', which is expected by the remoteKeyboard
// wire-format:
if (transEvent.key === Qt.Key_C)
transEvent.text = 'c';
if (transEvent.key === Qt.Key_V)
transEvent.text = 'v';
if (transEvent.key === Qt.Key_A)
transEvent.text = 'a';
if (transEvent.key === Qt.Key_X)
transEvent.text = 'x';
}
remoteKeyboard.sendQKeyEvent(transEvent); remoteKeyboard.sendQKeyEvent(transEvent);
event.accepted = true
} }
} }

View file

@ -8,6 +8,7 @@
#include "plugin_remotekeyboard_debug.h" #include "plugin_remotekeyboard_debug.h"
#include <KPluginFactory> #include <KPluginFactory>
#include <QDebug> #include <QDebug>
#include <QKeySequence>
#include <QString> #include <QString>
#include <QVariantMap> #include <QVariantMap>
@ -100,11 +101,23 @@ void RemoteKeyboardPlugin::sendKeyPress(const QString &key, int specialKey, bool
void RemoteKeyboardPlugin::sendQKeyEvent(const QVariantMap &keyEvent, bool sendAck) const void RemoteKeyboardPlugin::sendQKeyEvent(const QVariantMap &keyEvent, bool sendAck) const
{ {
if (!keyEvent.contains(QStringLiteral("key"))) if (!keyEvent.contains(QStringLiteral("key"))) {
return; return;
int k = translateQtKey(keyEvent.value(QStringLiteral("key")).toInt()); }
const int key = keyEvent.value(QStringLiteral("key")).toInt();
int k = translateQtKey(key);
int modifiers = keyEvent.value(QStringLiteral("modifiers")).toInt(); int modifiers = keyEvent.value(QStringLiteral("modifiers")).toInt();
sendKeyPress(keyEvent.value(QStringLiteral("text")).toString(),
// Qt will be calling xkb_state_key_get_utf8 to create this string.
// As documented, it will be giving us weird strings with Ctrl combinations:
// https://xkbcommon.org/doc/current/group__keysyms.html
// Instead, just use QKeySequence to tell us which key that is and move on
QString text = keyEvent.value(QStringLiteral("text")).toString();
if (!text.isEmpty() && !text[0].isLetterOrNumber()) {
text = QKeySequence(key).toString().toLower();
}
sendKeyPress(text,
k, k,
modifiers & Qt::ShiftModifier, modifiers & Qt::ShiftModifier,
modifiers & Qt::ControlModifier, modifiers & Qt::ControlModifier,