简体   繁体   English

QtQuick快捷方式中的处理修饰符

[英]Handling modifiers in QtQuick Shortcut

I have a simple QtQuick application, say 我说一个简单的QtQuick应用程序

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!" 当我按“ I”键时,我看到“已激活!” in the console. 在控制台中。 But my problem is my app should response to Alt key (without any main key). 但是我的问题是我的应用程序应该响应Alt键(没有任何主键)。 When I set "sequence" property to "Alt" (or "Ctrl", "Shift", ... any modifier) nothing happens. 当我将“ sequence”属性设置为“ Alt”(或“ Ctrl”,“ Shift”等任何修饰符)时,什么也不会发生。 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 我想我不能使用Keys.onPressed,因为无论现在重点放在哪个项目上,我都想处理Alt键

I don't think there is a way to do this on Windows, if that's where you're testing. 如果您正在测试Windows,我认为没有办法在Windows上实现。 It's been a problem for my application too, where the user should be able to hold Alt to use a colour picker. 这对于我的应用程序也是一个问题,用户应该能够按住Alt才能使用颜色选择器。 Works fine on macOS and Ubuntu though. 虽然可以在macOS和Ubuntu上正常工作。

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. 我的猜测是,它与alt是用于激活Windows上的菜单项的全局快捷方式有关。尽管对于Ubuntu来说也是如此,并且在这里也可以正常工作。

It seems to be an issue for eg Photoshop on Windows as well, with several common hacks to work around it: 对于Windows上的Photoshop来说,这似乎也是一个问题,有几种常见的解决方法:

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 ) Shortcut ,这需要一些不同的方法,可以使用附加的属性Keyshttp://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. 免责声明:由于修饰符有些不同,可能不会触发pressed信号,因此有少许机会无法使用。 In this case I hope I have pointed you at least in the right direction 在这种情况下,我希望我至少已指出正确的方向

Documentation says you can write sequence: "Alt+i" , but currently can not try it.... 文档说您可以编写sequence: "Alt+i" ,但是目前无法尝试...。

Have a nice day 祝你今天愉快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM