How can I capture QKeySequence from QKeyEvent depending on current keyboard layout?

蓝咒 提交于 2019-11-26 20:45:56

问题


I need to do this for configuring my application. I have QLineEdit field with reimplemented keyPressEvent method.

QKeyEvent *ke = ...
QString txt;

if(ke->modifiers() & Qt::ControlModifier)
    txt += "Ctrl+";
if(ke->modifiers() & Qt::AltModifier)
    txt += "Alt+";
if(ke->modifiers() & Qt::ShiftModifier)
    txt += "Shift+";

if(ke->key() >= Qt::Key_0 && ke->key() <= Qt::Key_9)
    txt += ('0' + ke->key() - Qt::Key_0);
else if(ke->key() >= Qt::Key_A && ke->key() <= Qt::Key_Z)
    txt += ('A' + ke->key() - Qt::Key_A);
ui->hotkeyEdit->setText(txt);

But this solution can create shortcuts only with english chars. For example when I use russian keyboard layout, this code will display the same sequence but with english char, placed on the same keyboard key.


回答1:


What you might find very useful is the function QKeySequence().toString().

The following is a part of a code that I use to capture User Defined Shortcuts. With some modifications it can do whatever you need in your project. Hope it helps (sorry for the crapped identation):

if (event->type() == QEvent::KeyPress){ 
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); 

    int keyInt = keyEvent->key(); 
    Qt::Key key = static_cast<Qt::Key>(keyInt); 
    if(key == Qt::Key_unknown){ 
        qDebug() << "Unknown key from a macro probably"; 
        return; 
    } 
    // the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta. 
    if(key == Qt::Key_Control || 
        key == Qt::Key_Shift || 
        key == Qt::Key_Alt || 
        key == Qt::Key_Meta)
    { 
        qDebug() << "Single click of special key: Ctrl, Shift, Alt or Meta"; 
        qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText); 
        return; 
    } 

    // check for a combination of user clicks 
    Qt::KeyboardModifiers modifiers = keyEvent->modifiers(); 
    QString keyText = keyEvent->text(); 
    // if the keyText is empty than it's a special key like F1, F5, ... 
    qDebug() << "Pressed Key:" << keyText; 

    QList<Qt::Key> modifiersList; 
    if(modifiers & Qt::ShiftModifier) 
        keyInt += Qt::SHIFT; 
    if(modifiers & Qt::ControlModifier) 
        keyInt += Qt::CTRL; 
    if(modifiers & Qt::AltModifier) 
        keyInt += Qt::ALT; 
    if(modifiers & Qt::MetaModifier) 
        keyInt += Qt::META; 

    qDebug() << "New KeySequence:" << QKeySequence(keyInt).toString(QKeySequence::NativeText); 
}



回答2:


If someone cares for the python version of the solution of Tihomir Dolapchiev above. I just translated it and it works great. Just posting for future reference.

class KeySequenceEdit(QtGui.QLineEdit):
    """
    This class is mainly inspired by
    http://stackoverflow.com/a/6665017

    """

    def __init__(self, keySequence, *args):
        super(KeySequenceEdit, self).__init__(*args)

        self.keySequence = keySequence
        self.setKeySequence(keySequence)

    def setKeySequence(self, keySequence):
        self.keySequence = keySequence
        self.setText(self.keySequence.toString(QtGui.QKeySequence.NativeText))


    def keyPressEvent(self, e):
        if e.type() == QtCore.QEvent.KeyPress:
            key = e.key()

            if key == QtCore.Qt.Key_unknown:
                warnings.warn("Unknown key from a macro probably")
                return

            # the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta.
            if(key == QtCore.Qt.Key_Control or
            key == QtCore.Qt.Key_Shift or
            key == QtCore.Qt.Key_Alt or
            key == QtCore.Qt.Key_Meta):
                print("Single click of special key: Ctrl, Shift, Alt or Meta")
                print("New KeySequence:", QtGui.QKeySequence(key).toString(QtGui.QKeySequence.NativeText))
                return

            # check for a combination of user clicks
            modifiers = e.modifiers()
            keyText = e.text()
            # if the keyText is empty than it's a special key like F1, F5, ...
            print("Pressed Key:", keyText)

            if modifiers & QtCore.Qt.ShiftModifier:
                key += QtCore.Qt.SHIFT
            if modifiers & QtCore.Qt.ControlModifier:
                key += QtCore.Qt.CTRL
            if modifiers & QtCore.Qt.AltModifier:
                key += QtCore.Qt.ALT
            if modifiers & QtCore.Qt.MetaModifier:
                key += QtCore.Qt.META

            print("New KeySequence:", QtGui.QKeySequence(key).toString(QtGui.QKeySequence.NativeText))

            self.setKeySequence(QtGui.QKeySequence(key))



回答3:


You can assign keyboard shortcuts to actions.

Here are someways to do it.

actionName->setShortcut(QKeySequence::New); //for predefined shortcuts like new, close, open..

or you can define your own shortcut with this

actionName->setShortcut(QKeySequence("Ctrl+N")); // sets Ctrl + N for w.e the action does

In the first case qt automatically detects and assigns the appropriate shortcut for that particular action. In the second case, you can pick your own desired shortcut and type it as string. It automatically parses it and understands.

This avoids the need for using key captures for unnecessary purposes.




回答4:


Prefer a standard seqence.

From the Keyboard Layout Issues:

As a result, both human-readable strings and hard-coded key codes can both be problematic to use when specifying a key sequence that can be used on a variety of different keyboard layouts. Only the use of standard shortcuts guarantees that the user will be able to use the shortcuts that the developer intended.




回答5:


simpliest way is:

if(event->type() == QEvent::KeyPress)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    qDebug() << "delete" << keyEvent->matches(QKeySequence::Delete);
    qDebug() << "copy" << keyEvent->matches(QKeySequence::Copy);
    qDebug() << "paste" << keyEvent->matches(QKeySequence::Paste);
}



回答6:


Trying to capture a QKeySequence in keyPressEvent might not work as expect. And I found a solution:

//  || event->type()== QEvent::ShortcutOverride is the key point
if (event->type() == QEvent::KeyPress || event->type()== QEvent::ShortcutOverride)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->matches(QKeySequence::Save))
    {
        // Do save
    }
}

I hope that helps someone. : )

Reference: QWidget::eventFilter() not catching key combinations



来源:https://stackoverflow.com/questions/6647970/how-can-i-capture-qkeysequence-from-qkeyevent-depending-on-current-keyboard-layo

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