问题
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