2018-11-08 01:01:51 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* 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>
|
2018-11-08 01:01:51 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2018-11-08 01:01:51 +00:00
|
|
|
*/
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
pragma ComponentBehavior: Bound
|
2018-11-08 01:01:51 +00:00
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
import QtQuick
|
|
|
|
import QtQuick.Controls as QQC2
|
2018-11-08 01:01:51 +00:00
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
import org.kde.kdeconnect as KDEConnect
|
|
|
|
|
|
|
|
QQC2.TextField {
|
2018-11-08 01:01:51 +00:00
|
|
|
id: root
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
property KDEConnect.DeviceDbusInterface device
|
|
|
|
|
2018-11-08 01:01:51 +00:00
|
|
|
readonly property alias available: checker.available
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
readonly property KDEConnect.PluginChecker pluginChecker: KDEConnect.PluginChecker {
|
2018-11-08 01:01:51 +00:00
|
|
|
id: checker
|
|
|
|
pluginName: "remotekeyboard"
|
2024-06-30 23:03:57 +01:00
|
|
|
device: root.device
|
2018-11-08 01:01:51 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
property KDEConnect.RemoteKeyboardDbusInterface remoteKeyboard
|
2018-11-08 01:01:51 +00:00
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
readonly property bool remoteState: available && remoteKeyboard ? remoteKeyboard.remoteState : false
|
2018-11-08 01:01:51 +00:00
|
|
|
|
|
|
|
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 {
|
2018-11-08 01:01:51 +00:00
|
|
|
//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
|
2018-11-08 01:01:51 +00:00
|
|
|
text = "";
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey === 4 // Left
|
|
|
|
&& cursorPosition > 0) {
|
2018-11-08 01:01:51 +00:00
|
|
|
--cursorPosition;
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey === 6 // Right
|
|
|
|
&& cursorPosition < text.length) {
|
2018-11-08 01:01:51 +00:00
|
|
|
++cursorPosition;
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey === 1) { // Backspace -> delete left
|
|
|
|
const pos = cursorPosition;
|
2018-11-08 01:01:51 +00:00
|
|
|
if (pos > 0) {
|
2024-06-30 23:03:57 +01:00
|
|
|
text = text.substring(0, pos - 1)
|
|
|
|
+ text.substring(pos, text.length);
|
2018-11-08 01:01:51 +00:00
|
|
|
cursorPosition = pos - 1;
|
|
|
|
}
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey === 13) { // Delete -> delete right
|
|
|
|
const pos = cursorPosition;
|
2018-11-08 01:01:51 +00:00
|
|
|
if (pos < text.length) {
|
2024-06-30 23:03:57 +01:00
|
|
|
text = text.substring(0, pos)
|
|
|
|
+ text.substring(pos + 1, text.length);
|
2018-11-08 01:01:51 +00:00
|
|
|
cursorPosition = pos; // seems to be set to text.length automatically!
|
|
|
|
}
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey === 10) { // Home
|
2018-11-08 01:01:51 +00:00
|
|
|
cursorPosition = 0;
|
2024-06-30 23:03:57 +01:00
|
|
|
} else if (specialKey == 11) { // End
|
2018-11-08 01:01:51 +00:00
|
|
|
cursorPosition = text.length;
|
2024-06-30 23:03:57 +01:00
|
|
|
} else {
|
2018-11-08 01:01:51 +00:00
|
|
|
// 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) {
|
2018-11-08 01:01:51 +00:00
|
|
|
sanitized += key.charAt(i);
|
2024-06-30 23:03:57 +01:00
|
|
|
}
|
2018-11-08 01:01:51 +00:00
|
|
|
}
|
|
|
|
if (sanitized.length > 0 && !ctrl && !alt) {
|
|
|
|
// insert sanitized at current pos:
|
2024-06-30 23:03:57 +01:00
|
|
|
const pos = cursorPosition;
|
2018-11-08 01:01:51 +00:00
|
|
|
text = text.substring(0, pos)
|
2024-06-30 23:03:57 +01:00
|
|
|
+ sanitized
|
|
|
|
+ text.substring(pos, text.length);
|
2018-11-08 01:01:51 +00:00
|
|
|
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);
|
2018-11-08 01:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
function sendEvent(event: KeyEvent): void {
|
2018-11-08 01:01:51 +00:00
|
|
|
if (remoteKeyboard) {
|
2024-06-30 23:03:57 +01:00
|
|
|
const transEvent = JSON.parse(JSON.stringify(event)); // transform to anonymous object
|
2018-11-08 01:01:51 +00:00
|
|
|
remoteKeyboard.sendQKeyEvent(transEvent);
|
2023-05-16 21:17:54 +01:00
|
|
|
event.accepted = true
|
2018-11-08 01:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:03:57 +01:00
|
|
|
Keys.onPressed: event => {
|
|
|
|
if (available) {
|
2018-11-08 01:01:51 +00:00
|
|
|
sendEvent(event);
|
2024-06-30 23:03:57 +01:00
|
|
|
}
|
2018-11-08 01:01:51 +00:00
|
|
|
event.accepted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
onAvailableChanged: {
|
2024-06-30 23:03:57 +01:00
|
|
|
if (available && device !== null) {
|
|
|
|
remoteKeyboard = KDEConnect.RemoteKeyboardDbusInterfaceFactory.create(device.id());
|
2018-11-08 01:01:51 +00:00
|
|
|
remoteKeyboard.remoteStateChanged.connect(remoteStateChanged);
|
|
|
|
} else {
|
2024-06-30 23:03:57 +01:00
|
|
|
remoteKeyboard = null;
|
2018-11-08 01:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|