qt 键盘 插件

http://www.cuteqt.com/bbs/viewthread.php?tid=509

插件实现________________________________
#ifndef KBLIBPLUGIN_H
#define KBLIBPLUGIN_H
#include <QKbdDriverPlugin>
class kblibPlugin : public QKbdDriverPlugin
{
    Q_OBJECT
public:
    kblibPlugin(QObject *parent = 0);
    QWSKeyboardHandler *create(const QString & key, const QString & device);
    QStringList keys () const;
};
#endif // KBLIBPLUGIN_H
____________________________________________
#include "kblibplugin.h"
#include "kblibhandler.h"
#include <QtDebug>
kblibPlugin::kblibPlugin(QObject *parent) :
        QKbdDriverPlugin(parent)
{
    qDebug()<<"plugin created";
}
QWSKeyboardHandler *kblibPlugin::create(
        const QString & key, const QString & device)
{
    qDebug()<<"plugin created";
    if(key.toLower()=="kblibhandler")
        return new kblibHandler(device);
    return 0;
}
QStringList kblibPlugin::keys() const
{
    return QStringList()<<"kblibhandler";
}
Q_EXPORT_PLUGIN2(kblibhandler, kblibPlugin)
______________________________________________
handler 的实现___________________________________
#ifndef KBLIBHANDLER_H
#define KBLIBHANDLER_H
#include <QWSKeyboardHandler>
class QSocketNotifier;
class kblibHandler:public QObject,public QWSKeyboardHandler
{
    Q_OBJECT
public:
    kblibHandler(const QString &device=QString("/dev/buttons"));
    ~kblibHandler();
private:
    QSocketNotifier *notify;
    int kbFd;
    char buttons[6];
private slots:
    void readData();
};
#endif // KBLIBHANDLER_H
__________________________________________
#include "kblibhandler.h"
#include "fcntl.h"
#include <QtCore>
#include <QDebug>
kblibHandler::kblibHandler(const QString &device)
{
    qDebug()<<"buttonpressed!";
    kbFd=open(device.toLocal8Bit().constData(),O_RDONLY,0);
    if(kbFd>=0){
        notify=new QSocketNotifier(kbFd,QSocketNotifier::Read,this);
        connect(notify,SIGNAL(activated(int)),this,SLOT(readData()));
    }else{
        qWarning("Cannot open %s for keyboard input",
                 device.toLocal8Bit().constData());
        return;
    }
}
kblibHandler::~kblibHandler()
{
    if(kbFd>=0)
        close(kbFd);
}
//SLOT, handle the buttons' change.
void kblibHandler::readData()
{
    Qt::KeyboardModifiers modifiers = Qt::NoModifier; //enum of Modifiers
    int currentKey=-1;
    int unicode=0xffff;
    int keyCode=0;
    if(read(kbFd,buttons,sizeof buttons)!=sizeof buttons){
        qWarning("Cannot read buttons");
        return;
    }
    //keyword 'unsigned' eleminated Complier's warning!
    for(unsigned int i=0;i<(sizeof buttons/sizeof buttons[0]);i++){
        if(buttons==1){
            currentKey=i;
            break;
        }
    }
    //if the button released, then return;
    if(currentKey==-1)
        return;
    switch(currentKey){
    case 0:
        keyCode=Qt::Key_Display;
        unicode=0xffff;
        break;
    case 1:
        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
        break;
    case 5:
        break;
    default:
        return;
    }
    //Sends a key event to the Qt for Embedded Linux server application.
    processKeyEvent(unicode,keyCode,modifiers,true,false);
}
_______________________________________
Qt Pro__________________________________
QT += core \
    gui
TARGET = kblibhandler
TEMPLATE = lib
CONFIG += plugin
#DESTDIR = $$[QT_INSTALL_PLUGINS]/kbddrivers
SOURCES += kblibplugin.cpp \
    kblibhandler.cpp
HEADERS += kblibplugin.h \
    kblibhandler.h
______________________________________
QWS变量设置成:QWS_KEYBOARD=kblibhandler:/dev/buttons,插件我也放到正确的plugins目录:plugins/kbddrivers/,可是就无法load出来,插件我是按照这个来做的http://qt.nokia.com/doc/qtopia4.3/tut-deviceexample.html

运行程序之前 export QT_DEBUG_PLUGINS=1
可以看到插件载入过程中的错误。

posted on 2011-08-19 17:00  katago  阅读(1738)  评论(0编辑  收藏  举报