Capturing modifier keys Qt

不羁的心 提交于 2019-12-05 07:56:48

Try using this to check for shift:

if(event->modifiers() & Qt::ShiftModifier){...}

this to check for control:

if(event->modifiers() & Qt::ControlModifier){...}

and so on. That works well for me.

EDIT:

To get the modifiers of a wheel event, you need to check the QWheelEvent object passed to your wheelEvent() method:

void MainWindow::wheelEvent( QWheelEvent *wheelEvent )
{
    if( wheelEvent->modifiers() & Qt::ShiftModifier )
    {
        // do something awesome
    }
    else if( wheelEvent->modifiers() & Qt::ControlModifier )
    {
        // do something even awesomer!
    }
}
Pavel Strakhov

According to the documentation, QKeyEvent::modifiers cannot always be trusted. Try to use QApplication::keyboardModifiers() static function instead.


From Qt 5 Doc. – Qt::KeyboardModifiers QKeyEvent::modifiers() const:

Warning: This function cannot always be trusted. The user can confuse it by pressing both Shift keys simultaneously and releasing one of them, for example.

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