kdeconnect-kde/declarativeplugin/qml/RemoteKeyboard.qml

109 lines
4.1 KiB
QML
Raw Normal View History

/**
* SPDX-FileCopyrightText: 2017 Holger Kaelberer <holger.k@elberer.de>
2024-06-30 23:03:57 +01:00
* SPDX-FileCopyrightText: 2024 ivan tkachenko <me@ratijas.tk>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
2024-06-30 23:03:57 +01:00
pragma ComponentBehavior: Bound
2024-06-30 23:03:57 +01:00
import QtQuick
import QtQuick.Controls as QQC2
2024-06-30 23:03:57 +01:00
import org.kde.kdeconnect as KDEConnect
QQC2.TextField {
id: root
2024-06-30 23:03:57 +01:00
property KDEConnect.DeviceDbusInterface device
readonly property alias available: checker.available
2024-06-30 23:03:57 +01:00
readonly property KDEConnect.PluginChecker pluginChecker: KDEConnect.PluginChecker {
id: checker
pluginName: "remotekeyboard"
2024-06-30 23:03:57 +01:00
device: root.device
}
2024-06-30 23:03:57 +01:00
property KDEConnect.RemoteKeyboardDbusInterface remoteKeyboard
2024-06-30 23:03:57 +01:00
readonly property bool remoteState: available && remoteKeyboard ? remoteKeyboard.remoteState : false
Connections {
2024-06-30 23:03:57 +01:00
target: root.remoteKeyboard
function onKeyPressReceived(key: string, specialKey: int, shift: bool, ctrl: bool, alt: bool): void {
//console.log("XXX received keypress key=" + key + " special=" + specialKey + " shift=" + shift + " ctrl=" + ctrl + " text=" + text + " cursorPos=" + cursorPosition);
// interpret some special keys:
2024-06-30 23:03:57 +01:00
if (specialKey === 12 || specialKey === 14) { // Return/Esc -> clear
text = "";
2024-06-30 23:03:57 +01:00
} else if (specialKey === 4 // Left
&& cursorPosition > 0) {
--cursorPosition;
2024-06-30 23:03:57 +01:00
} else if (specialKey === 6 // Right
&& cursorPosition < text.length) {
++cursorPosition;
2024-06-30 23:03:57 +01:00
} else if (specialKey === 1) { // Backspace -> delete left
const pos = cursorPosition;
if (pos > 0) {
2024-06-30 23:03:57 +01:00
text = text.substring(0, pos - 1)
+ text.substring(pos, text.length);
cursorPosition = pos - 1;
}
2024-06-30 23:03:57 +01:00
} else if (specialKey === 13) { // Delete -> delete right
const pos = cursorPosition;
if (pos < text.length) {
2024-06-30 23:03:57 +01:00
text = text.substring(0, pos)
+ text.substring(pos + 1, text.length);
cursorPosition = pos; // seems to be set to text.length automatically!
}
2024-06-30 23:03:57 +01:00
} else if (specialKey === 10) { // Home
cursorPosition = 0;
2024-06-30 23:03:57 +01:00
} else if (specialKey == 11) { // End
cursorPosition = text.length;
2024-06-30 23:03:57 +01:00
} else {
// echo visible keys
2024-06-30 23:03:57 +01:00
let sanitized = "";
for (let i = 0; i < key.length; i++) {
if (key.charCodeAt(i) > 31) {
sanitized += key.charAt(i);
2024-06-30 23:03:57 +01:00
}
}
if (sanitized.length > 0 && !ctrl && !alt) {
// insert sanitized at current pos:
2024-06-30 23:03:57 +01:00
const pos = cursorPosition;
text = text.substring(0, pos)
2024-06-30 23:03:57 +01:00
+ sanitized
+ text.substring(pos, text.length);
cursorPosition = pos + 1; // seems to be set to text.length automatically!
}
}
2024-06-30 23:03:57 +01:00
// console.log("XXX After received keypress key=" + key + " special=" + specialKey + " shift=" + shift + " ctrl=" + ctrl + " text=" + text + " cursorPos=" + cursorPosition);
}
}
2024-06-30 23:03:57 +01:00
function sendEvent(event: KeyEvent): void {
if (remoteKeyboard) {
2024-06-30 23:03:57 +01:00
const transEvent = JSON.parse(JSON.stringify(event)); // transform to anonymous object
remoteKeyboard.sendQKeyEvent(transEvent);
event.accepted = true
}
}
2024-06-30 23:03:57 +01:00
Keys.onPressed: event => {
if (available) {
sendEvent(event);
2024-06-30 23:03:57 +01:00
}
event.accepted = true;
}
onAvailableChanged: {
2024-06-30 23:03:57 +01:00
if (available && device !== null) {
remoteKeyboard = KDEConnect.RemoteKeyboardDbusInterfaceFactory.create(device.id());
remoteKeyboard.remoteStateChanged.connect(remoteStateChanged);
} else {
2024-06-30 23:03:57 +01:00
remoteKeyboard = null;
}
}
}