Check if a Key is Down with Qt

So, I need to poll the keyboard in my Paint() routine. I haven't found how to do it with Qt. I thought of having a map which would be updated in keyPressEvent and keyReleaseEvent and poll that map in Paint().

Your second method is what I would have done, except that I would use a continuous, periodic QTimer event to poll the keyboard-pressed map and call QWidget::Update() function when necessary to invalidate the display widget instead. Performing non-painting operations inside Paint() is strongly discouraged for many reasons but I do not know how to explain that well.


This doesn't solve the general problem of detecting which keys are pressed, but if you are only looking for keyboard modifiers (shift, ctrl, alt, etc.), you can retrieve that through the static QApplication::keyboardModifiers() and QApplication::queryKeyboardModifiers() methods.