0

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

3 Answers3

1

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:

Mitch
  • 23,716
  • 9
  • 83
  • 122
0

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

Amfasis
  • 3,932
  • 2
  • 18
  • 27
  • That's what I talk about. It's even possible to use "if(event.key === Qt.Key_Alt)", but i want to handle this key from whatever Item is in focus now. Moreover, I want my forceActiveFocus() onto my item after Alt was pressed... – Roman Sverdlov Feb 01 '19 at 13:49
  • Then I think it's not feasible. – Amfasis Feb 01 '19 at 14:31
0

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

Have a nice day

Florian Schmidt
  • 369
  • 3
  • 13