在Qt/Embedded 2.3.8中添加MX21Ads键盘处理

本文介绍如何在Qt中自定义键盘处理类QWSMx21ButtonsHandler,通过继承QWSKeyboardHandler并重写相关方法来实现特定硬件的键盘事件处理。文章提供了完整的类定义及构造函数、读取键盘数据等关键部分的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经过查看源代码发现Qt的键盘处理是判断环境变量QWS_KEYBOARD再加载相应的键盘处理拦截类, 而qt已经包含了几个典型的键盘处理,只要以它们为蓝本即可编写自己的处理类! 一下我以 vr4xx为模板, 建立QWSMx21ButtonsHandler键盘处理类! 以下是相关实现代码,

2. 添加键盘处理类
class QWSMx21ButtonsHandler : public QWSKeyboardHandler
{
Q_OBJECT
public:
QWSMx21ButtonsHandler();
virtual QWSMx21ButtonsHandler();

bool isOpen() { return buttonFD > 0; }

private slots:
void readKeyboardData(); // 这个函数最重要, 包含的读键盘扫描码的处理

private:
QString terminalName;
int buttonFD;
int kbdIdx;
int kbdBufferLen;
unsigned char *kbdBuffer;
QSocketNotifier *notifier;
};
/*
* mx21 buttons driver
*/

QWSMx21ButtonsHandler::QWSMx21ButtonsHandler() : QWSKeyboardHandler()
{
terminalName = "/dev/buttons";
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;
}

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

void QWSMx21ButtonsHandler::readKeyboardData()
{

int n = 0;
do {
n = read(buttonFD, kbdBuffer+kbdIdx, kbdBufferLen - kbdIdx );
if ( n > 0 )
kbdIdx += n;
} while ( n > 0 );

int idx = 0;
while ( kbdIdx - idx >= 2 ) {
unsigned char *next = kbdBuffer + idx;
unsigned short *code = (unsigned short *)next;

if ( ( *code & 0x8000) != 0x8000) {
idx += 2;
continue;
}
int keycode = Qt::Key_unknown;
//在此添加键盘扫描码映射关系
switch ( (*code) & 0x00ff ) {
case 59:
keycode = Qt::Key_Up;
break;
default:
qDebug("Unrecognised key sequence %d", (int)code );
}
processKeyEvent( 0, keycode, 0, TRUE, FALSE );
idx += 2;
}

int surplus = kbdIdx - idx;
for ( int i = 0; i < surplus; i++ )
kbdBuffer[i] = kbdBuffer[idx+i];
kbdIdx = surplus;
}

2. 添加加载代码
src/kernel/qkeyboard_qws.cpp
/*
* keyboard driver instantiation
*/

QWSKeyboardHandler *QWSServer::newKeyboardHandler( const QString &spec )
{
QWSKeyboardHandler *handler = 0;

QString device;
QString type;
int colon=spec.find(':');
if ( colon>=0 ) {
type = spec.left(colon);
device = spec.mid(colon+1);
} else {
type = spec;
}

if ( type == "Buttons" ) {
#if defined(QT_QWS_YOPY)
handler = new QWSyopyButtonsHandler();
#elif defined(QT_QWS_CASSIOPEIA)
handler = new QWSVr41xxButtonsHandler();
#endif
} else if ( type == "QVFbKeyboard" ) {
handler = new QWSVFbKeyboardHandler();
} else if ( type == "USB" ) {
handler = new QWSUsbKeyboardHandler(device);
} else if ( type == "TTY" ) {
handler = new QWSTtyKeyboardHandler(device);
} else if ( type == "MX21" ) { //这是我们添加的MX21键盘处理类
handler = new QWSMx21ButtonsHandler();
} else {
qWarning( "Keyboard type %s:%s unsupported", spec.latin1(), device.latin1() );
}

return handler;
}
3.重新编译链接
在目标平台上只要设置QWS_KEYBOARD=MX21即可在界面中拦截到键盘事件处理!
这里只是给出了基本原理实现,具体的键盘扫瞄码映射关系得看具体的操作!
最新的qtopia-core 版本也可以参考Vr4xx 实现来做, 在src/gui/embedded/下!

希望对有需要的人有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值