javascriptqmlsignalskeyboard-events

QML Keys.onDeletePressed not registering


I'm trying to handle a QML/JavaScript situation where the user presses the delete key. For some reason, Keys.onDeletePressed: {} is not firing. I am getting the signal for onEscapePressed, onSpacePressed, and onReturnPressed, so I don't think it's a focus issue, or some other setup issue. I can't figure out why it's only the delete key that's not working. Is this a bug?

        Keys.onReturnPressed: {
            // code that does stuff deleted.
            console.log("Return Pressed.");
        }


        Keys.onEscapePressed: {
            // code that does stuff deleted.
            console.log("Escape pressed.");
        }


        Keys.onDeletePressed: {
            console.log("delete pressed");
        }


        Keys.onSpacePressed: {
            console.log("space pressed.");
        }

Solution

  • It could be you're pressing Backspace, here's a more robust check:

    Keys.onPressed: (event) => {
        switch (event.key) {
        case Qt.Key_Return:
            console.log("Return Pressed.");
            break;
        case Qt.Key_Escape:
            console.log("Escape Pressed.");
            break;
        case Qt.Key_Delete:
            console.log("Delete Pressed.");
            break;
        case Qt.Key_Backspace:
            console.log("Backspace Pressed.");
            break;
        case Qt.Key_Space:
            console.log("Space Pressed.");
            break;
        default:
            console.log("Unknown key " + event.key + " pressed");
            break;
        }
    }