问题
I'm currently learning Qt by developing a simple desktop app with Python and QML. The document about Pyside2
with QML
is surprisingly insufficient.
Anyway, now I'm struggling with user custom shortcut key registering.
Some app have a text field in which you can press a key combination and that combination will show in the text field. something like this image
According to the official document, there's a QKeySequenceEdit in C++.
Is there any way to do this in QML
? Or at least in Pyside2
?
Or maybe QML is not a good choice for GUI from beginning?
update
strange characters using @folibis's answer
Ctrl+d`, Ctrl+c, Ctrl+v left to right.
update 2 I ended up with code below based on @folibis's answer:
var keyModifiers = [
{"name": "Ctrl", "code": Qt.ControlModifier},
{"name": "Shift", "code": Qt.ShiftModifier},
{"name": "Alt", "code": Qt.AltModifier},
{"name": "Meta", "code": Qt.MetaModifier},
// {"name": "Keypad", "code": Qt.KeypadModifier},
{"name": "GroupSwitch", "code": Qt.GroupSwitchModifier}
]
var numpadKeycodes = [
{"name": "Del", "code": Qt.Key_Period},
{"name": "+", "code": Qt.Key_Plus},
{"name": "*", "code": Qt.Key_Asterisk},
{"name": "-", "code": Qt.Key_Minus},
{"name": "/", "code": Qt.Key_Slash},
{"name": "0", "code": Qt.Key_0},
{"name": "1", "code": Qt.Key_1},
...
]
var keycodes = [
...
{"name": "/", "code": Qt.Key_Question}, // ?
{"name": "2", "code": Qt.Key_At}, // @
{"name": "A", "code": Qt.Key_A},
{"name": "B", "code": Qt.Key_B},
{"name": "C", "code": Qt.Key_C},
....
]
TextField {
id: txt
readOnly: true
Keys.onPressed: {
var str = "";
var text;
keyModifiers.forEach(modifier => {
if(event.modifiers & modifier.code) {
str += `${modifier.name} + `;
}
});
// Numpad keys
if (event.modifiers & Qt.KeypadModifier) {
str += "Num "
text = numpadKeycodes.some(keycode => {
if (keycode.code === event.key) {
str += keycode.name;
return keycode.name
}
});
} else {
text = keycodes.some(keycode => {
if (keycode.code === event.key) {
str += keycode.name;
return keycode.name
}
});
}
if (str === "" && !text) {
str = "None"
}
txt.text = str;
}
Keys.onReleased: {
if (txt.text.endsWith(" ")) {
txt.text = "None"
}
}
}
Job's done but too much code... If there's a better way, please let me know.
回答1:
The beauty of QML is that you can easy do whatever you want. As for your issue you can implement something like the following:
TextField {
id: txt
readOnly: true
anchors.centerIn: parent
Keys.onPressed: {
var str = '';
var arr = {
'Shift': Qt.ShiftModifier,
'Ctrl': Qt.ControlModifier,
'Alt': Qt.AltModifier,
'Meta': Qt.MetaModifier,
'Keypad': Qt.KeypadModifier,
'GroupSwitch': Qt.GroupSwitchModifier };
for(var key in arr) {
var value = arr[key];
if(event.modifiers & value) {
str += (str === '' ? '' : '-') + key;
}
}
if(event.text !== '')
str += (str === '' ? '' : '-') + event.text;
txt.text = str;
}
}
来源:https://stackoverflow.com/questions/61983774/qml-how-to-register-user-custom-shortcut