Important: Please read the Qt Code of Conduct -

MouseArea with different behaviour if Control-Key is pressed

  • Hi,

    I have a Mouse area, and depending on whether modifier keys like alt or control are pressed, the behaviour should change. What is the best way to do this?

    Have on mouse area and have clauses like


    Although my naive if clause does not even work in my example:

            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton
                onPressed: {
                        if(Qt.ControlModifier) {
                            recZoom.x = mouseX;
                            recZoom.y = mouseY;
                            recZoom.visible = true;

    Here the rectangle does not become visible if I press the Control key and the left button. How can I make this work? Also, if I have an item which comes with its own mouse area and is on top of this mouse area, how would this be handled?

  • Ok, the solution is probably here:

    I tried

            mouse.modifiers & Qt.NoModifier

    to test for no modifier keys but this does not work.

  • You need something like this

            onPressed: {
                if (mouse.modifiers & Qt.ControlModifier) {
                    console.log("Mouse area pressed with control")

    missed it by that much :)

  • @mranger90 Thanks anyway :-)

Log in to reply