Search code examples
c++qtqmlqtvirtualkeyboard

Qt VirtualKeyboard enabling and disabling keys


I am writing an application that uses Qt 5.6.3 and QtVirtualKeyboard, and I need to be able to enable/disable its keys. I managed to do it by manually editing the layout files, but I need to do it dynamically, depending on user input.

I have InputPanel that I am using in my qml file like so

InputPanel {
    id: inputPanel
    visible: true
    y: parent.height - inputPanel.height
    anchors.left: parent.left
    anchors.right: parent.right
}

Keyboard with all keys enabled

This is the default keyboard with all keys enabled.

QWERT disabled

And here Q/W/E/R/T are disabled.

How can I disable VirtualKeyboard keys like that in either c++ or qml?


Solution

  • Based on this accepted answer (Hide key from Qt Virtual keyboard), I can propose this one which disables manually the 'm' key:

    import QtQuick 2.11
    import QtQuick.Controls 2.3
    import QtQuick.VirtualKeyboard 2.1
    import "content"
    
    Item {
        width: 1280
        height: 720
    
        property var keyboardLayout: inputPanel.keyboard.layout
    
        function disableKey(parent, objectText) {
            var obj = null
            if (parent === null)
                return null
            var children = parent.children
            for (var i = 0; i < children.length; i++) {
                obj = children[i]
                if (obj.text === objectText && obj.toString().substring(0, 7) === "BaseKey") {
                    obj.enabled = false
                }
                obj = disableKey(obj, objectText)
                if (obj)
                    break
            }
            return obj
        }
    
        onKeyboardLayoutChanged: {
            if (keyboardLayout !== "") {
                disableKey(inputPanel.keyboard, 'm')
            }
        }
    
        InputPanel {
            id: inputPanel
            anchors.fill: parent
        }
    }