关于qte3.3.8键盘问题,请高手指教?
最近在2410上移植qte3.3.8,交叉编译后把相关库下到开发板上,程序可以运行了,但是键盘没有反映,我板子上原来用的是qte2.3.7版本,键盘都可以正常使用,
不知道为什么换成qte3.3.8之后就没有反映了,下面是我在网上找到的加入自定义键盘的方法,有不太明白的地方,请哪位大哥帮忙看一下,小弟先谢过了!
下面是自定义键盘接口:需要改动的地方有4个,在qt-arm/src/embedded 目录下
1.增加一个mykbd_qws.h,内容如下:
#ifndef MYKBD_QWS_H
#define MYKBD_QWS_H
#include "qkbd_qws.h" //QT定义抽象基类QWSKeyboardHandler
#ifndef QT_NO_MYKBD //编译时可以通过定义这个变量从而不编译这个模块
class MyKbdPrivate; //我们的键盘设备私有类,实现具体的键盘设备操作,如打开键盘、读键盘数据,解析按键等等。
class MyKbdHandler : public QWSKeyboardHandler //供系统调用的键盘句柄
{
public:
MyKbdHandler(const QString&);
virtual ~MyKbdHandler();
private:
MyKbdPrivate *d;
};
#endif // QT_NO_MYKBD
#endif // MYKBD_QWS_H
2.增加一个mykbd_qws.cpp,内容如下:
#include "mykbd_qws.h"
#ifndef QT_NO_MYKBD
#include
#include
#include
#include
#include
#include
#include
#include
class MyKbdPrivate : public QObject
{
Q_OBJECT
public:
MyKbdPrivate( MyKbdHandler *h, const QString& );
virtual ~MyKbdPrivate();
bool isOpen() { return buttonFD > 0; }
private slots:
void readKeyboardData();
private:
QString terminalName;
int buttonFD;
int kbdIdx;
// int kbdBufferLen;
unsigned char kbdBuffer;
QSocketNotifier *notifier;
MyKbdHandler *handler;
};
MyKbdPrivate::MyKbdPrivate(MyKbdHandler *h, const QString &device ) : handler(h)
{
terminalName = device.isEmpty()?"/dev/mykeyboard":device.latin1();
buttonFD = -1;
notifier = 0;
if ((buttonFD = open(terminalName, O_RDWR | O_NDELAY, 0)) < 0)
{
qWarning("Cannot open %s\n", terminalName.latin1());
}
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;
}
MyKbdPrivate::~ MyKbdPrivate()
{
if ( buttonFD > 0 ) {
::close( buttonFD );
buttonFD = -1;
}
delete notifier;
notifier = 0;
//delete [] kbdBuffer;
}
void MyKbdPrivate::readKeyboardData()
{
int n = 0;
int idx = 0;
n = read(buttonFD, &kbdBuffer, sizeof(kbdBuffer));
if(n!=1)
return;
int keycode = Qt::Key_unknown;
switch ( (kbdBuffer) & 0xff ) {
case 1:
keycode = Qt::Key_Left;
break;
case 2:
keycode = Qt::Key_Right;
break;
case 3:
keycode = Qt::Key_Tab;
break;
case 4:
keycode = Qt::Key_Backtab;
break;
case 5:
keycode = Qt::Key_Enter;
break;
case 6:
keycode = Qt::Key_Return;
break;
default:
qDebug("Unrecognised key code %d", kbdBuffer );
}
handler->processKeyEvent( 0, keycode, 0, TRUE, FALSE );
}
MyKbdHandler::MyKbdHandler(const QString &device)
{
d = new MyKbdPrivate( this, device );
}
MyKbdHandler::~MyKbdHandler()
{
delete d;
}
#include "mykbd_qws.moc"
#endif // QT_NO_MYKBD
kbdBuffer的大小和switch()语句大家可以根据自己的驱动程序作调整
注意:n=read(buttonFD, &kbdBuffer, sizeof(kbdBuffer));这里我的kbdBuffer是一个char型,这里的read每次读回一个char型,所以我的驱动程序里read函数也要每次返回一个char字符,6个按键返回的值分别是1,2,3,4,5,6,对应的键为:Qt::Key_Left, Qt::Key_Right, Qt::Key_Tab, Qt::Key_Backtab, Qt::Key_Enter, Qt::Key_Return。
3.修改qkbddriverfactory_qws.cpp中的QWSKeyboardHandler *QKbdDriverFactory::create(),添加自己的定义的接口
其中qkbddriverfactory_qws.cpp如下:注有//mykeyboard 表示是我添加的语句
QWSKeyboardHandler *QKbdDriverFactory::create( const QString& key, const QString& device )
{
QString driver = key.lower();
#ifndef QT_NO_MYKBD //mykeyboard
if ( driver == "mykbd" || driver.isEmpty() )
return new MyKbdHandler( device );
#endif
#ifdef Q_OS_QNX6
if ( driver == "qnx" || driver.isEmpty() )
return new QWSQnxKeyboardHandler( device );
#endif
...
return 0;
}
QStringList QKbdDriverFactory::keys()
{
QStringList list;
#ifdef Q_OS_QNX6
if ( !list.contains( "Qnx" ) )
list