Multiple keys



  • i want a multilpe key e.g. shift+a but i have a problem and i cant handle it.
    here is a part of my code
    could you help me?
    thanks a lot

    case Qt::Key_Shift:
    if(event->modifiers().testFlag(Qt::ShiftModifier))
    {
    msg->setText("you pressed : shift + "+event1->text());
    msg->exec();
    }



  • Qt::Key_Shift is key modifier and not a key by itself so comparing it in a switch case will not work, instead do somthing like this
    @case Qt::Key_A:
    if(event->modifiers().testFlag(Qt::ShiftModifier))
    qDebug() << "Shift + A"
    else
    qDebug() << "A";@



  • it didnt work.
    i want to handle all the buttons & multiple ones of keyboard.
    i mean...i want to handle "a" , "shift+a" , "ctrl+a" , ect.
    how can i handle it???



  • make a bitwise AND

    @if (event->modifiers() & Qt::ShiftModifier) {
    //do something
    } else {
    //do another thing
    }@



  • Here is an example.
    @class KeySpy : public QObject
    {
    Q_OBJECT
    public:
    explicit KeySpy(QObject * = 0) { }

    protected:
    bool eventFilter(QObject *, QEvent * event);
    };

    bool KeySpy::eventFilter(QObject *, QEvent * event)
    {
    if(event->type() != QEvent::KeyPress)
    return false;

    QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
    
    int k = keyEvent->key();
    char key_string = '?';
    
    if((Qt::Key_0 <= k) && (k <= Qt::Key_9))
    {
        key_string = k - Qt::Key_0 + '0';
    }
    else if((Qt::Key_A <= k) && (k <= Qt::Key_Z))
    {
        key_string = k - Qt::Key_A + 'A';
    }
    
    QString key;
    if(keyEvent->modifiers().testFlag(Qt::ShiftModifier))
    {
        key = QString("Shitf + %1").arg(key_string);
    }
    else if(keyEvent->modifiers().testFlag(Qt::ControlModifier))
    {
        key = QString("Ctrl + %1").arg(key_string);
    }
    else if(keyEvent->modifiers().testFlag(Qt::AltModifier))
    {
        key = QString("Alt + %1").arg(key_string);
    }
    else
    {
        key = QString("%1").arg(key_string);
    }
    
    qDebug() << key << keyEvent->text();
    
    return false;
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.