Cours:QtEvdevConsole
Révision datée du 14 octobre 2022 à 11:24 par Bjacquot (discussion | contributions) (Page créée avec « {|style="vertical-align:middle; width:100%; text-align:left; " |- | {{boîte déroulante/début|titre=consolereader.h}} <source lang=cpp> #ifndef... »)
#ifndef CONSOLEREADER_H
#define CONSOLEREADER_H
#include <QObject>
#include <QSocketNotifier>
struct ExampleInput {
unsigned int dummy1;
unsigned int dummy2;
unsigned short type;
unsigned short code;
unsigned int value;
};
class ConsoleReader : public QObject
{
Q_OBJECT
public:
explicit ConsoleReader(QObject *parent = 0);
signals:
void getEvent(unsigned short code);
public slots:
void text();
private:
QSocketNotifier * notifier;
int kbdFd;
};
#endif // CONSOLEREADER_H
|
#include "consolereader.h"
#include <QTextStream>
#include <QDebug>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> //Provides STDIN_FILENO
ConsoleReader::ConsoleReader(QObject *parent) :
QObject(parent)
{
kbdFd = ::open("/dev/input/event1", O_RDONLY, 0);
if (kbdFd >= 0) {
notifier = new QSocketNotifier(kbdFd, QSocketNotifier::Read, this);
connect(notifier, SIGNAL(activated(int)), this, SLOT(text()));
}
else
{
qDebug()<<"marche pas, pas le bon périphérique ?? ";
}
}
void ConsoleReader::text()
{
ExampleInput event;
int n = read(kbdFd, &event, sizeof(ExampleInput));
if (n != 16)
{
qDebug() << "erreur ???" << n;
return;
}
else
{
if ((event.type==1)&&(event.value==0))
{
qDebug() << "code evdev =" << event.code;
emit getEvent(event.code);
}
}
}
|