自定义4*4矩阵键盘在Qt4程序中的使用方法

时间:2021-11-18 00:12:15
解压qt-everywhere-opensource-src-4.7.0.tgz 到当前目录

1. 修改qt-everywhere-opensource-src-4.7.0/src/gui/embedded/src/gui/embedded/qkbdtty_qws.h,其内容如下:
#ifndef QKBDTTY_QWS_H

#define QKBDTTY_QWS_H

#include<QtGui/qkbd_qws.h>

#ifndef QT_NO_QWS_KEYBOARD

#ifndef QT_NO_QWS_KBD_TTY


class QWSMyKbPrivate;


class QWSMyKeyboardHandler : publicQWSKeyboardHandler

{

public:

QWSMyKeyboardHandler(const QString&);

virtual ~QWSMyKeyboardHandler();


private:


QWSMyKbPrivate *d;

};


#endif // QT_NO_QWS_KBD_TTY


#endif // QT_NO_QWS_KEYBOARD


#endif // QKBDTTY_QWS_H

2. 修改qt-everywhere-opensource-src-4.7.0/src/gui/embedded/qkbdtty_qws.cpp,其内容如下:

#include "qkbdtty_qws.h"


#if !defined(QT_NO_QWS_KEYBOARD)&& !defined(QT_NO_QWS_KBD_TTY)


#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>


class QWSMyKbPrivate : publicQObject

{

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, constQString &device)

: m_handler(h)

{

terminalName =device.isEmpty()?"/dev/atao_button":device.toLatin1();

buttonFD = -1;

notifier = 0;

if ((buttonFD =QT_OPEN(terminalName.toLatin1().constData(), O_RDONLY | O_NDELAY))< 0)

{

qWarning("Cannot open %s\n",terminalName.toLatin1());

}

printf("open /dev/atao_buttonOK!\tbuttonFD=%d\n",buttonFD);

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;;

}


voidQWSMyKbPrivate::readKeyboardData()

{

int n = 0;

int idx = 0;

n = read(buttonFD, kbdBuffer+kbdIdx,4);

unsigned char *next = kbdBuffer +idx;

int *code = (int *)next;

int keycode = Qt::Key_unknown;

int unicode = 0;

switch ( (*code) &0xff )

{

case 0:

keycode = Qt::Key_0;

unicode='0';

break;

case 1:

keycode = Qt::Key_1;

unicode='1';

break;

case 2:

keycode = Qt::Key_2;

unicode='2';

break;

case 3:

keycode = Qt::Key_3;

unicode='3';

break;

case 4:

keycode = Qt::Key_4;

unicode='4';

break;

case 5:

keycode = Qt::Key_5;

unicode='5';

break;

case 6:

keycode = Qt::Key_6;

unicode='6';

break;

case 7:

keycode = Qt::Key_7;

unicode='7';

break;

case 8:

keycode = Qt::Key_8;

unicode='8';

break;

case 9:

keycode = Qt::Key_9;

unicode='9';

break;

case 10:

keycode = Qt::Key_Enter;

break;

case 11:

keycode = Qt::Key_Escape;

break;

case 12:

keycode = Qt::Key_Tab;

break;

case 13:

keycode = Qt::Key_Backspace;

break;

case 14:

keycode = Qt::Key_Right;

break;

case 15:

keycode = Qt::Key_Left;

break;


default:

qDebug("Unrecognised key code %d",*code );

}

m_handler->processKeyEvent( unicode, keycode, 0,TRUE, FALSE );

m_handler->processKeyEvent( unicode, keycode, 0,FALSE, FALSE);

//m_handler->processKeyEvent( 0, keycode, 0, TRUE,FALSE );

}


#include "qkbdtty_qws.moc"

#endif // QT_NO_QWS_KEYBOARD ||QT_NO_QWS_KBD_TTY


注意,此处,我的键盘驱动设备为/dev/atao_button,使用了4*4矩阵键盘,定义如下图,所要,要先写好键盘驱动啦。





3. qt-everywhere-opensource-src-4.7.0目录中运行如下configure 命令


./configure -prefix/usr/src/qt470arm.kb.4.2.2 -opensource -confirm-license -release-shared -embedded arm -xplatform qws/linux-arm-g++ -no-qt3support-fast -no-largefile -make tools -make demos -make examples -makedocs -qt-libjpeg -qt-libpng -qt-libtiff -qt-gif -multimedia

4. make make install

重新grep "QWSMyKeyboardHandler" * -R会得到如下结果表明添加自定义按键驱动成功


ttseibm@ttseibm-T60:~/qt-everywhere-opensource-src-4.7.0$ grep"QWSMyKeyboardHandler" * -R

Binary file lib/libQtGui.so.4.7matches

Binary file lib/libQtGui.somatches

Binary file lib/libQtGui.so.4.7.0matches

Binary file lib/libQtGui.so.4matches

src/gui/embedded/qkbdtty_qws.h:classQWSMyKeyboardHandler : public QWSKeyboardHandler

src/gui/embedded/qkbdtty_qws.h:QWSMyKeyboardHandler(const QString &);

src/gui/embedded/qkbdtty_qws.h:virtual ~QWSMyKeyboardHandler();

src/gui/embedded/qkbdtty_qws.cpp:QWSMyKbPrivate(QWSMyKeyboardHandler *handler, const QString&device);

src/gui/embedded/qkbdtty_qws.cpp:QWSMyKeyboardHandler *m_handler;

src/gui/embedded/qkbdtty_qws.cpp:QWSMyKeyboardHandler::QWSMyKeyboardHandler(constQString &device)

src/gui/embedded/qkbdtty_qws.cpp:QWSMyKeyboardHandler::~QWSMyKeyboardHandler()

src/gui/embedded/qkbdtty_qws.cpp:QWSMyKbPrivate::QWSMyKbPrivate(QWSMyKeyboardHandler*h, const QString &device)

src/gui/embedded/qkbddriverfactory_qws.cpp: return newQWSMyKeyboardHandler(device);

Binary filesrc/gui/embedded/backup.tar matches

Binary filesrc/gui/.obj/release-shared-emb-arm/qkbddriverfactory_qws.omatches

Binary filesrc/gui/.obj/release-shared-emb-arm/qkbdtty_qws.o matches

ttseibm@ttseibm-T60:~/qt-everywhere-opensource-src-4.7.0$


5. Qt SDK 开发程序啦。。