Key pressed in QWidget::mousePressedEvent()



  • Is there any way that let me know if the spacebar is being pressed inside QWidget::mousePressEvent() / QWidget::mouseMoveEvent() ?



  • @M-Wong
    You may try this

    void Widget::mousePressEvent(QMouseEvent *e)
    {
            if(e->button()==Qt::Key_Space) {
             e->accept();
        }
    }


  • @Ratzz

    Thanks for your reply.
    However I don't think your code will tell whether the SpaceBar is being pressed or not inside the mousePressEvent()...

    Anyway, I now use Win32 API -- GetKeyState() / GetAsyncKeyState() to solve the problem.


  • Qt Champions 2016

    @M-Wong

    Is there any way that let me know if the spacebar is being pressed inside QWidget::mousePressEvent() / QWidget::mouseMoveEvent() ?

    Not to my knowledge, no. But you can intercept the key-press event as well as the mouse event and set a state internally that the spacebar is pressed.

    Anyway, I now use Win32 API -- GetKeyState() / GetAsyncKeyState() to solve the problem.

    That's possible, but the implications would be you code will be working only(!) on windows and you would require the windows SDKs to build it.

    Kind regards.



  • @kshegunov said:

    Not to my knowledge, no. But you can intercept the key-press event as well as the mouse event and set a state internally that the spacebar is pressed.

    Good Advice. I may try it... But I should stop programming now and get back to my real hobby -- 3DCG. ( Programming isn't even my hobby, I just need some app that's tailor-made for myself.


Log in to reply
 

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