#include#include int main() { sf::RenderWindow window(sf::VideoMode(200, 200), "SFML window"); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::KeyPressed) { std::cout << "Key pressed: " << event.key.code << std::endl; event.key.consume(); // consume the key event } if (event.type == sf::Event::Closed) { window.close(); } } window.display(); } return 0; }
#includeIn this example, we are using the Qt library with the consume function. Whenever a key is pressed, we print the code of the key and then consume the event by calling the accept function. Package library: The SFML library for example 1 and the Qt library for example 2.#include #include #include class MyWidget : public QWidget { public: MyWidget(QWidget* parent = nullptr) : QWidget(parent) { } protected: void keyPressEvent(QKeyEvent* event) override { std::cout << "Key pressed: " << event->key() << std::endl; event->accept(); // consume the key event } }; int main(int argc, char** argv) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); }