Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Correct InputMethod for QtQuick.VirtualKeyboard / qtvirtualkeyboard



  • Hello, I want to use the QtQuick.VirtualKeyboard. When I use it in the following way, I can use all features of the virtual keyboard:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.VirtualKeyboard 2.15
    
    Window {
        id: window
        width: 640
        height: 480
        visible: true
    
        TextEdit {
            id: textEdit
            height: 30
            anchors.fill: parent
        }
    
        InputPanel {
            id: inputPanel
            z: 99
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            width: window.width
        }
    }
    
    

    But I want to write a custom input method, like described here: https://doc.qt.io/qt-6/technical-guide.html#implementing-a-custom-input-method
    I tried to implement this in the following way:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.VirtualKeyboard 2.15
    
    Window {
        id: window
        width: 640
        height: 480
        visible: true
    
        TextEdit {
            id: textEdit
            height: 30
            anchors.fill: parent
            enabled: false
        }
    
        InputPanel {
            id: inputPanel
            z: 99
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            width: window.width
        }
    
        InputMethod {
            id: customInputMethod
    
            function inputModes(locale) {
                return [InputEngine.Cyrillic, InputEngine.Latin, InputEngine.Numeric];
            }
            function selectionLists() {
                return [];
            }
    
            function setInputMode(locale, inputMode) {
                return true
            }
    
            function setTextCase(textCase) {
                return true
            }
    
            function reset() {
                // TODO: reset the input method without modifying input context
            }
    
            function update() {
                // TODO: commit current state and update the input method
            }
    
            function keyEvent(key, text, modifiers) {
                var accept = true
                textEdit.text = text
                return accept;
            }
        }
    
        Component.onCompleted: {
            InputContext.inputEngine.inputMethod = customInputMethod
        }
    }
    

    But compared with the built-in input method, the virtual keyboard has now only a subset of the features. Most important for me is, that the shift key is not working, means i can only write lower case letters. Has somebody an idea to enable the shift key?

    I guess it could be solved by setting InputContext.inputMethodHints to something unequal zero, but this property is readonly. Is there a way to manipulate this property without using a text input field (the problem is that I would have to struggle with the focus of this text input field, and I dont need a text input).

    When I debug the first code snipped I can observe that InputContext.inputEngine.inputMethod has the value PlainInputMethod, but I can not find this input method as qml type (to override the keyEvent() method for my purposes).