C++ (Qt)QKeyEvent::modifiers () const
C++ (Qt)quint32 QKeyEvent::nativeModifiers () const
C++ (Qt)while(!asleep()) sheep++;
C++ (Qt)#include <QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
C++ (Qt)#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include <QLabel>#include <QSet> class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); signals: public slots: protected: bool eventFilter(QObject *obj, QEvent *event); void updateKeys(); private: QLabel *label; bool upKey, downKey, rightKey, leftKey, fireKey; }; #endif // MAINWINDOW_H
C++ (Qt)#include "mainwindow.h"#include <QEvent>#include <QKeyEvent> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ label = new QLabel(this); installEventFilter(this); setCentralWidget(label); upKey = downKey = rightKey = leftKey = fireKey = false;} bool MainWindow::eventFilter(QObject *obj, QEvent *event){ if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { QKeyEvent* ke = static_cast<QKeyEvent*>(event); bool b; event->type() == QEvent::KeyPress ? b = true : b = false; switch(ke->key()) { case Qt::Key_Up: upKey = b; break; case Qt::Key_Down: downKey = b; break; case Qt::Key_Right: rightKey = b; break; case Qt::Key_Left: leftKey = b; break; case Qt::Key_Space: fireKey = b; break; } updateKeys(); return true; } return QObject::eventFilter(obj, event);} void MainWindow::updateKeys(){ QString text; if (upKey) text.append("Up "); if (downKey) text.append("Down "); if (rightKey) text.append("Right "); if (leftKey) text.append("Left "); if (fireKey) text.append("Fire "); label->setText(text);}
event->type() == QEvent::KeyPress ? b = true : b = false;
C++ (Qt)if (event->type() == QEvent::KeyPress) b = true; else b = false;
C++ (Qt)bool b = event->type() == QEvent::KeyPress;