Warning, /network/kdeconnect-kde/declarativeplugin/qml/RemoteKeyboard.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * SPDX-FileCopyrightText: 2017 Holger Kaelberer <holger.k@elberer.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 import QtQuick 2.1
0008 import org.kde.kdeconnect 1.0
0009 import QtQuick.Controls 2.4
0010 
0011 TextField {
0012 
0013     id: root
0014 
0015     property alias device: checker.device
0016     readonly property alias available: checker.available
0017 
0018     readonly property PluginChecker pluginChecker: PluginChecker {
0019         id: checker
0020         pluginName: "remotekeyboard"
0021     }
0022 
0023     property var remoteKeyboard: null
0024 
0025     readonly property bool remoteState: available ? remoteKeyboard.remoteState : false
0026 
0027     Connections {
0028         target: remoteKeyboard
0029         function onKeyPressReceived() {
0030             //console.log("XXX received keypress key=" + key + " special=" + specialKey + " shift=" + shift + " ctrl=" + ctrl + " text=" + text + " cursorPos=" + cursorPosition);
0031             // interpret some special keys:
0032             if (specialKey == 12 || specialKey == 14)  // Return/Esc -> clear
0033                 text = "";
0034             else if (specialKey == 4  // Left
0035                      && cursorPosition > 0)
0036                 --cursorPosition;
0037             else if (specialKey == 6  // Right
0038                      && cursorPosition < text.length)
0039                 ++cursorPosition;
0040             else if (specialKey == 1) {    // Backspace -> delete left
0041                 var pos = cursorPosition;
0042                 if (pos > 0) {
0043                     text = text.substring(0, pos-1)
0044                                                + text.substring(pos, text.length);
0045                     cursorPosition = pos - 1;
0046                 }
0047             } else if (specialKey == 13) {    // Delete -> delete right
0048                 var pos = cursorPosition;
0049                 if (pos < text.length) {
0050                         text = text.substring(0, pos)
0051                           + text.substring(pos+1, text.length);
0052                     cursorPosition = pos; // seems to be set to text.length automatically!
0053                 }
0054             } else if (specialKey == 10)    // Home
0055                 cursorPosition = 0;
0056             else if (specialKey == 11)    // End
0057                 cursorPosition = text.length;
0058             else {
0059                 // echo visible keys
0060                 var sanitized = "";
0061                 for (var i = 0; i < key.length; i++) {
0062                     if (key.charCodeAt(i) > 31)
0063                         sanitized += key.charAt(i);
0064                 }
0065                 if (sanitized.length > 0 && !ctrl && !alt) {
0066                     // insert sanitized at current pos:
0067                     var pos = cursorPosition;
0068                     text = text.substring(0, pos)
0069                                                + sanitized
0070                                                + text.substring(pos, text.length);
0071                     cursorPosition = pos + 1; // seems to be set to text.length automatically!
0072                 }
0073             }
0074 //            console.log("XXX After received keypress key=" + key + " special=" + specialKey + " shift=" + shift + " ctrl=" + ctrl + " text=" + text + " cursorPos=" + cursorPosition);
0075         }
0076     }
0077 
0078 
0079     function sendEvent(event) {
0080         if (remoteKeyboard) {
0081             var transEvent = JSON.parse(JSON.stringify(event)); // transform to anonymous object
0082             remoteKeyboard.sendQKeyEvent(transEvent);
0083             event.accepted = true
0084         }
0085     }
0086 
0087     Keys.onPressed: {
0088         if (available)
0089             sendEvent(event);
0090         event.accepted = true;
0091     }
0092 
0093     onAvailableChanged: {
0094         if (available) {
0095             remoteKeyboard = RemoteKeyboardDbusInterfaceFactory.create(device.id());
0096             //remoteKeyboard.keyPressReceived.connect(keyPressReceived);
0097             remoteKeyboard.remoteStateChanged.connect(remoteStateChanged);
0098         } else {
0099             remoteKeyboard = null
0100         }
0101     }
0102 }