开发板自定义键盘驱动for QT

 对于开发板上的QT程序来说需要接收键盘的按键,本项目中使用的是自定义键盘。所以对于QT来说需要识别自定义键盘的上的按键。

大概流程图如下:

开发板自定义键盘驱动for QT_第1张图片

 

step-1:

 在 qt-everywhere-opensource-src-4.6.3/src/gui/embedded 下增加如下两个文件

---------------------------------

qkbd_my_qws.h

+++++++++++++++++

#ifndef QKBD_MY_QWS_H
#define QKBD_MY_QWS_H

#include <QtGui/qkbd_qws.h>

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Gui)

#ifndef QT_NO_QWS_KEYBOARD


#ifndef QT_NO_QWS_KBD_MY
class QWSMYKbPrivate;

class QWSMYKeyboardHandler : public QWSKeyboardHandler
{
public:
    QWSMYKeyboardHandler(const QString &);
    virtual ~QWSMYKeyboardHandler();
private:
    QWSMYKbPrivate *d;
};
#endif // QT_NO_QWS_KBD_MY

#endif // QT_NO_QWS_KEYBOARD

QT_END_NAMESPACE

QT_END_HEADER

#endif // QKBD_MY_QWS_H

 

---------------------------------

--------------------------------

qkbd_my_qws.cpp

++++++++++++++

#include "qkbd_my_qws.h"

#ifndef QT_NO_QWS_KBD_MY
#include <QSocketNotifier>
#include <QStringList>
#include <QString>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <private/qcore_unix_p.h>
#include <qsocketnotifier.h>
#include <linux/input.h>


QT_BEGIN_NAMESPACE

class QWSMYKbPrivate : public QObject
{
    Q_OBJECT
public:
    QWSMYKbPrivate(QWSMYKeyboardHandler *handler, const QString &device);
    ~QWSMYKbPrivate();
    bool isOpen() { return buttonFD > 0; }

private Q_SLOTS:
    void readKeyboardData();

private:
    QWSMYKeyboardHandler                    *m_handler;
    QString                                 terminalName;
    int                                         buttonFD;
    int                                         kbdIdx;
    int                                         kbdBufferLen;
    unsigned char                           *kbdBuffer;
    QSocketNotifier                         *notifier;
};

QWSMYKeyboardHandler::QWSMYKeyboardHandler(const QString &device)
    : QWSKeyboardHandler(device)
{
    d = new QWSMYKbPrivate(this, device);
}

QWSMYKeyboardHandler::~QWSMYKeyboardHandler()
{
    delete d;
}

QWSMYKbPrivate::QWSMYKbPrivate(QWSMYKeyboardHandler *h, const QString &device)
    : m_handler(h)
{
        terminalName = device.isEmpty()?"/dev/input/event1":device;//device.toLatin1();
        buttonFD = -1;
        notifier = 0;
        if ((buttonFD = QT_OPEN(terminalName.toLatin1().constData(), O_RDONLY | O_NDELAY)) < 0)
        {
                qWarning("Cannot open %s\n", terminalName);//terminalName.toLatin1());
        }
    printf("Open MY usr keyboard success!!\n");
        if ( buttonFD >= 0 )
        {
                notifier = new QSocketNotifier( buttonFD, QSocketNotifier::Read, this );
                connect( notifier, SIGNAL(activated(int)),this,SLOT(readKeyboardData()) );
        }
        kbdBufferLen = 80;
        kbdBuffer = new unsigned char [kbdBufferLen];
        kbdIdx = 0;
}

QWSMYKbPrivate::~ QWSMYKbPrivate()
{
        if ( buttonFD > 0 )
        {
                ::close( buttonFD );
                buttonFD = -1;
        }
        delete notifier;
        notifier = 0;
        delete [] kbdBuffer;;
}

void QWSMYKbPrivate::readKeyboardData()
{
    struct  input_event event;
   // InputData ; //

    int n = read(buttonFD, &event, sizeof(struct input_event));

    if (n != sizeof(struct input_event))
    {
        printf("key pressed: n=%d\n", n);
        return;
    }
    Qt::KeyboardModifiers modifiers = Qt::NoModifier;
    int unicode = 0xffff;
    int key_code = 0;
   
    if(EV_KEY == event.type)
    {
    // 可以根据自己特定的硬件值来设定。
    switch (event.code)
    {

    case 0x25:
            key_code = Qt::Key_0;
            unicode = 0xffff;
            break;

    case 0x6a:
            key_code = Qt::Key_1;
            unicode = 0xffff;
            break;


    case 0x1E:
        key_code = Qt::Key_F1;
        unicode = 0xffff;
        break;

    case 0x24:
        key_code = Qt::Key_F2;
        unicode = 0xffff;
        break;


    default:
        qDebug("default : %d\n",event.code);
        break;

    }

    m_handler->processKeyEvent(unicode, key_code, modifiers, event.value != 0, false);
   }

   if (EV_SYN == event.type)
    printf ("syn event\n\n");

}

QT_END_NAMESPACE

#include "qkbd_my_qws.moc"

#endif    // QT_NO_QWS_KBD_MY

--------------------------------

 

step-2 :

在qkbddriverfactory_qws.cpp文件中 :

#include "qkbd_my_qws.h"

.....

QWSKeyboardHandler *QKbdDriverFactory::create(const QString& key, const QString& device)
{
    QString driver = key.toLower();
#ifndef QT_NO_QWS_KBD_MY

    if (driver == QLatin1String("my") || driver.isEmpty())
    {
 printf("tsc keyboard");
        return new QWSMYKeyboardHandler(device);
    }
#endif

.......

}

 

....

QStringList QKbdDriverFactory::keys()
{
    QStringList list;
#ifndef QT_NO_QWS_KBD_MY
    list << QLatin1String("my");
#endif

...........

}

step-3 :

修改qt-everywhere-opensource-src-4.6.3/目录下的 configure

 做如下修改
        CFG_KBD_AVAILABLE="my tty linuxinput qvfb"
        CFG_KBD_ON="my" #default, see QMakeVar above------ 原来是tty
       
        #QMakeVar add kbd-drivers "tty"---原来的
        QMakeVar add kbd-drivers "my

 

重新configure ,增加 -qt-kbd-my选项

 

step-4:

进入 qt-everywhere-opensource-src-4.6.3/src/gui/embedded
   在这个目录下 make -f Makefile

  make install

 

step-5:

然后把编译出来的 libQtGui* 库文件拷贝到开发板的文件系统的库文件路径下(/usr/lib)
   cp ~/Qt/output/qt-arm/lib/libQtGui* ~/targetfs/usr/lib/

 

完成~~~~~

 

这样就可以接收自定义按键的消息了。

你可能感兴趣的:(struct,delete,input,qt,makefile,keyboard)