//keypadDriverPlugin.h文件 class KeypadDriverPlugin : public QKbdDriverPlugin { Q_OBJECT public: KeypadDriverPlugin(QObject *parent = 0); ~KeypadDriverPlugin(); QWSKeyboardHandler *create(const QString &driver, const QString &device); QWSKeyboardHandler *create(const QString &driver); QStringList keys() const; }; //类的实现文件 KeypadDriverPlugin::KeypadDriverPlugin(QObject *parent) { } KeypadDriverPlugin::~KeypadDriverPlugin() { } QStringList KeypadDriverPlugin::keys() const { return QStringList() << "OptimusKpdHandler"; } // // 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 *KeypadDriverPlugin::create(const QString &driver, const QString &device) { PR("KeypadDriverPlugin::create###################################: %s\n", driver.toLocal8Bit().constData()); if (driver.toLower() == "optimuskpdhandler") { PR("Before creating KeypadHandler\n"); return new KeypadHandler(device); } return 0; } QWSKeyboardHandler *KeypadDriverPlugin::create(const QString &driver) { if (driver.toLower() == "optimuskpdhandler") { PR("Before creating KeypadHandler"); return new KeypadHandler(); } return 0; } //keypadHandler.h文件 class KeypadHandler : public QObject, public QWSKeyboardHandler { Q_OBJECT public: KeypadHandler(const QString &device = QString("/dev/input/event0")); ~KeypadHandler(); private: QSocketNotifier *m_notifier; int kbdFd; private slots: void readKpdData(); }; //类的实现文件 struct InputData { unsigned int dummy1; unsigned int dummy2; unsigned short type; unsigned short code; unsigned int value; unsigned int dummy3; unsigned int dummy4; unsigned int dummy5; unsigned int dummy6; }; KeypadHandler::KeypadHandler(const QString &device) { setObjectName("Optimus Keypad Handler"); this->kbdFd = ::open(device.toLocal8Bit().constData(), O_RDONLY, 0); if (kbdFd > 0) { PR("%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 { PR("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; } } KeypadHandler::~KeypadHandler() { if (kbdFd > 0) ::close(kbdFd); } // Key function void KeypadHandler::readKpdData() { InputData event; int n = read(kbdFd, &event, sizeof(InputData)); if (n != sizeof(InputData)) { PR("key pressed: n=%d\n", n); g_Log.Debugf(LOG_DETAIL_TRACE, 0, L"key pressed: n=%d", n); return; } PR("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); }