How can I let users use control + + for zoom in and control + - for zoom out?

旧城冷巷雨未停 提交于 2019-12-31 05:32:06

问题


The problem with my code below is that on US/UK keyboard layouts + is generated with shift + =, but when the user uses both the control and shift modifiers simultaneously, + is not generated. This has been tested on Mac.

    Keys.onPressed: {
        if (event.modifiers & Qt.ControlModifier) {
            if (event.key === Qt.Key_Minus) {
                zoom(false)
                event.accepted = true
            } else if (event.key === Qt.Key_Plus) {
                zoom(true)
                event.accepted = true
            }
        }
    }

Since control + + and control + - are standard shortcuts for zooming in applications I am certain that others have solved this. But how?


回答1:


Instead of Key.onPressed use Shortcut and its sequence property :

Shortcut {
    sequence: StandardKey.ZoomIn
    onActivated: zoom(true)
}

Your issue is mentionned in this section of the QKeySequence documentation.




回答2:


You have to use Qt.ShiftModifier for reacting on shift key:

Item {
    focus: true
    Keys.onPressed: {
        if ((event.key == Qt.Key_Plus) && (event.modifiers & Qt.ShiftModifier))
            console.log("PRessed");
    }
}


来源:https://stackoverflow.com/questions/53775916/how-can-i-let-users-use-control-for-zoom-in-and-control-for-zoom-out

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!