How can I let users use control + + for zoom in an

2019-03-03 23:14发布

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?

标签: qt qml
2条回答
forever°为你锁心
2楼-- · 2019-03-03 23:29

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");
    }
}
查看更多
不美不萌又怎样
3楼-- · 2019-03-03 23:36

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.

查看更多
登录 后发表回答