Roman Sverdlov
Roman Sverdlov

Reputation: 93

Handling modifiers in QtQuick Shortcut

I have a simple QtQuick application, say

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    Shortcut {
        sequence: "i"
        context: Qt.ApplicationShortcut
        onActivated: {
            console.log("activated!")
        }
    }
}

When I press the "I" key i see "activated!" in the console. But my problem is my app should response to Alt key (without any main key). When I set "sequence" property to "Alt" (or "Ctrl", "Shift", ... any modifier) nothing happens. So is there any way to handle only modifier pressing within the shortcut?

I think I can not use Keys.onPressed because I want to handle Alt key no matter which Item is focused now

Upvotes: 0

Views: 683

Answers (3)

Mitch
Mitch

Reputation: 24416

I don't think there is a way to do this on Windows, if that's where you're testing. It's been a problem for my application too, where the user should be able to hold Alt to use a colour picker. Works fine on macOS and Ubuntu though.

My guess is that it has something to do with alt being a global shortcut for activating menu items on Windows.. though the same is true for Ubuntu, and it works fine there.

It seems to be an issue for e.g. Photoshop on Windows as well, with several common hacks to work around it:

Upvotes: 1

Florian Schmidt
Florian Schmidt

Reputation: 379

Documentation says you can write sequence: "Alt+i", but currently can not try it....

Have a nice day

Upvotes: 0

Amfasis
Amfasis

Reputation: 4198

This requires a bit different approach then Shortcut, you can use the attached property Keys (http://doc.qt.io/qt-5/qml-qtquick-keyevent.html)

Item {
    focus: true
    Keys.onPressed: { 
        if (event.modifiers & Qt.AltModifier)
             console.log("alt activated")
    }
}

Disclaimer: there is a slight chance it will not work since the modifiers are a bit different and maybe don't trigger the pressed signal. In this case I hope I have pointed you at least in the right direction

Upvotes: 0

Related Questions