//键盘插件的实现
//MyKeyDriverPlugin.h文件
class MyKeyDriverPlugin : public QKbdDriverPlugin
{
Q_OBJECT
public:
MyKeyDriverPlugin(QObject *parent = 0);
~MyKeyDriverPlugin();
QWSKeyboardHandler *create(const QString &driver, const QString &device);
QWSKeyboardHandler *create(const QString &driver);
QStringList keys() const;
};
//插件类的实现文件
MyKeyDriverPlugin::MyKeyDriverPlugin(QObject *parent)
{
}
MyKeyDriverPlugin::~MyKeyDriverPlugin()
{
}
QStringList MyKeyDriverPlugin::keys() const
{
return QStringList() << "i2c";
}
//
// The create() functions are responsible for returning an instance of
// the keypad driver. We do this only if the driver parameter matches our key.
//
QWSKeyboardHandler *MyKeyDriverPlugin::create(const QString &driver, const QString &device)
{
qDebug("MyKeyDriverPlugin::create###: %s\n",driver.toLocal8Bit().constData());
if (driver.toLower() == "i2c")
{
qDebug("Before creating MyKeyHandler\n");
return new MyKeyHandler(device);
}
return 0;
}
QWSKeyboardHandler *MyKeyDriverPlugin::create(const QString &driver)
{
if (driver.toLower() == "i2c")
{
qDebug("Before creating MyKeyHandler");
return new MyKeyHandler();
}
return 0;
}
//键盘驱动的实现
//MyKeyHandler.h文件
class MyKeyHandler : public QObject, public QWSKeyboardHandler
{
Q_OBJECT
public:
MyKeyHandler(const QString &device = QString("/dev/i2c-1"));
~MyKeyHandler();
private:
QSocketNotifier *m_notifier;
int kbdFd;
private slots:
void readKpdData();
};
//类的实现文件
struct InputData
{
unsigned int dummy1;
unsigned short type;
unsigned short code;
unsigned int value;
unsigned int dummy2;
};
MyKeyHandler::MyKeyHandler(const QString &device)
{
setObjectName("I2C Keypad Handler");
this->kbdFd = ::open(device.toLocal8Bit().constData(), O_RDONLY, 0);
if (kbdFd > 0)
{
qDebug("%s opened as keyboard input.\n", device.toLocal8Bit().constData());
//g_Log.Debugf(LOG_DETAIL_TRACE, 0, L"%s opened as keyboard input.", device.toLocal8Bit().constData());
this->m_notifier = new QSocketNotifier(kbdFd, QSocketNotifier::Read, this);
connect(this->m_notifier, SIGNAL(activated(int)), this, SLOT(readKpdData()));
}
else
{
qDebug("Cannot open %s for keyboard input. (%s)",
device.toLocal8Bit().constData(), strerror(errno));
//g_Log.Errorf(LOG_DETAIL_IMPORTANT, 0, L"Cannot open %s for keyboard input. (%s)",
// device.toLocal8Bit().constData(), strerror(errno));
return;
}
}
MyKeyHandler::~MyKeyHandler()
{
if (kbdFd > 0)
::close(kbdFd);
}
// Key function
void MyKeyHandler::readKpdData()
{
InputData event;
int n = read(kbdFd, &event, sizeof(InputData));
if (n != sizeof(InputData))
{
qDebug("key pressed: n=%d\n", n);
//g_Log.Debugf(LOG_DETAIL_TRACE, 0, L"key pressed: n=%d", n);
return;
}
qDebug("key pressed: type=%d, code=0x%x, value=%d, %s\n",
event.type, event.code, event.value, (event.value != 0)? "(Down)" : "(Up)");
//g_Log.Debugf(LOG_DETAIL_TRACE, 0, L"key pressed: type=%d, code=%d, value=%d, %s",
// event.type, event.code, event.value, (event.value != 0)? "(Down)" : "(Up)");
Qt::KeyboardModifiers modifiers = Qt::NoModifier;
int unicode = 0xffff;
int key_code = 0;
// 可以根据自己特定的硬件值来设定。
switch (event.code)
{
case 0x2:
key_code = Qt::Key_1;
unicode = '1';
break;
case 0x110:
key_code = Qt::Key_Context1;
unicode = 0xffff;
break;
case 0x100:
key_code = Qt::Key_Back;
unicode = 0xffff;
break;
default:
break;
}
this->processKeyEvent(unicode, key_code, modifiers, event.value != 0, false);
}
Qt键盘驱动处理类的实现:Qt键盘驱动处理类的实现,通过Qt的插件机制实现嵌入式开发中的自定义键盘处理。
最新推荐文章于 2024-06-17 16:26:19 发布