1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <QtGui>
class myFilter : public QObject
{
myFilter()
{
for (int i=0;i<4;++i)
EtatClavier[i] = false;
qApp->installEventFilter(this);
}
myFilter(myFilter&);
void operator=(myFilter&);
public :
static
const myFilter &instance()
{
static myFilter filter;
return filter;
}
enum direction
{
HAUT =0,
GAUCHE,
BAS,
DROIT
};
bool EtatClavier[4];
void setActive(int key, bool etat)
{
if (key == Qt::Key_Z)
{
EtatClavier[HAUT] = etat;
}
if (key == Qt::Key_Q)
{
EtatClavier[GAUCHE] = etat;
}
if (key == Qt::Key_S)
{
EtatClavier[BAS] = etat;
}
if (key == Qt::Key_D)
{
EtatClavier[DROIT] = etat;
}
}
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ( !keyEvent->isAutoRepeat())
{
setActive(keyEvent->key(),true);
}
}
if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ( !keyEvent->isAutoRepeat())
{
setActive(keyEvent->key(),false);
}
}
if (event->type() == QEvent::ApplicationDeactivate) {
for (int i=0;i<4;++i)
EtatClavier[i] = false;
}
return QObject::eventFilter(obj, event);
}
};
class maWidget : public QWidget
{
Q_OBJECT
QCheckBox *cHaut;
QCheckBox *cBas;
QCheckBox *cGauche;
QCheckBox *cDroit;
QTimer refreshTimer;
public :
maWidget()
{
QGridLayout *l =new QGridLayout;
{
cHaut = new QCheckBox("haut");
cHaut->setEnabled(false);
cBas = new QCheckBox("bas");
cBas->setEnabled(false);
cGauche = new QCheckBox("gauche");
cGauche->setEnabled(false);
cDroit = new QCheckBox("droit");
cDroit->setEnabled(false);
l->addWidget(cHaut,0,1);
l->addWidget(cBas,2,1);
l->addWidget(cGauche,1,0);
l->addWidget(cDroit,1,2);
}
setLayout(l);
refreshTimer.start(20);
connect(&refreshTimer,SIGNAL(timeout()),this,SLOT(refresh()));
}
public slots:
void refresh()
{
cHaut->setChecked ( myFilter::instance().EtatClavier[myFilter::HAUT]);
cBas->setChecked ( myFilter::instance().EtatClavier[myFilter::BAS]);
cGauche->setChecked ( myFilter::instance().EtatClavier[myFilter::GAUCHE]);
cDroit->setChecked ( myFilter::instance().EtatClavier[myFilter::DROIT]);
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
maWidget w;
w.show();
QTextEdit b;
b.show();
return app.exec();
} |
Partager