⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qkeyboard_qws.cpp

📁 qt里加入自己的按键驱动
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/* USB driver */QWSUsbKeyboardHandler::QWSUsbKeyboardHandler(const QString& device){    fd = open(device.isEmpty()?"/dev/input/event0":device.latin1(),O_RDONLY, 0);    if ( fd >= 0 ) {	QSocketNotifier *notifier;	notifier = new QSocketNotifier( fd, QSocketNotifier::Read, this );	connect( notifier, SIGNAL(activated(int)),this,		 SLOT(readKeyboardData()) );    }}QWSUsbKeyboardHandler::~QWSUsbKeyboardHandler(){    close(fd);}struct Myinputevent {    unsigned int dummy1;    unsigned int dummy2;    unsigned short type;    unsigned short code;    unsigned int value;};void QWSUsbKeyboardHandler::readKeyboardData(){    Myinputevent event;    int n = read(fd, &event, sizeof(Myinputevent) );    if ( n != 16 )	return;    int key=event.code;    if(key==103) {	processKeyEvent( 0, Qt::Key_Up, 0, event.value!=0, false );    } else if(key==106) {	processKeyEvent( 0, Qt::Key_Right, 0, event.value!=0, false  );    } else if(key==108) {	processKeyEvent( 0, Qt::Key_Down, 0, event.value!=0, false );    } else if(key==105) {	processKeyEvent( 0, Qt::Key_Left, 0, event.value!=0, false );    } else {	if(event.value==0) {	    key=key | 0x80;	}	doKey(key);    }}/* * YOPY buttons driver * Contributed by Ron Victorelli (victorrj at icubed.com) */QWSyopyButtonsHandler::QWSyopyButtonsHandler() : QWSKeyboardHandler(){#ifdef QT_QWS_YOPY    terminalName = "/dev/tty1";    buttonFD = -1;    notifier = 0;    if ((buttonFD = open(terminalName, O_RDWR | O_NDELAY, 0)) < 0) {	qFatal("Cannot open %s\n", terminalName.latin1());    } else {       tcsetpgrp(buttonFD, getpgid(0));       /* put tty into "straight through" mode.       */       if (tcgetattr(buttonFD, &oldT) < 0) {           qFatal("Linux-kbd: tcgetattr failed");       }       newT = oldT;       newT.c_lflag &= ~(ICANON | ECHO  | ISIG);       newT.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);       newT.c_iflag |= IGNBRK;       newT.c_cc[VMIN]  = 0;       newT.c_cc[VTIME] = 0;       if (tcsetattr(buttonFD, TCSANOW, &newT) < 0) {           qFatal("Linux-kbd: TCSANOW tcsetattr failed");       }       if (ioctl(buttonFD, KDSKBMODE, K_MEDIUMRAW) < 0) {           qFatal("Linux-kbd: KDSKBMODE tcsetattr failed");       }	notifier = new QSocketNotifier( buttonFD, QSocketNotifier::Read, this );	connect( notifier, SIGNAL(activated(int)),this,		 SLOT(readKeyboardData()) );    }#endif}QWSyopyButtonsHandler::~QWSyopyButtonsHandler(){#ifdef QT_QWS_YOPY    if ( buttonFD > 0 ) {	::close( buttonFD );	buttonFD = -1;    }#endif}void QWSyopyButtonsHandler::readKeyboardData(){#ifdef QT_QWS_YOPY    uchar buf[1];    char c='1';    int fd;    int n=read(buttonFD,buf,1);    if (n<0) {	qDebug("Keyboard read error %s",strerror(errno));    } else {	uint code = buf[0]&YPBUTTON_CODE_MASK;        bool press = !(buf[0]&0x80);        // printf("Key=%d/%d/%d\n",buf[1],code,press);        int k=(-1);        switch(code) {          case 39:       k=Qt::Key_Up;     break;          case 44:       k=Qt::Key_Down;   break;          case 41:       k=Qt::Key_Left;   break;          case 42:       k=Qt::Key_Right;  break;          case 56:       k=Qt::Key_F1;     break; //windows          case 29:       k=Qt::Key_F2;     break; //cycle          case 24:       k=Qt::Key_F3;     break; //record          case 23:       k=Qt::Key_F4;     break; //mp3          case 4:        k=Qt::Key_F5;     break; // PIMS          case 1:        k=Qt::Key_Escape; break; // Escape          case 40:       k=Qt::Key_Up;     break; // prev          case 45:       k=Qt::Key_Down;   break; // next          case 35:       if( !press ) {                           fd = open("/proc/sys/pm/sleep",O_RDWR,0);                           if( fd >= 0 ) {                               write(fd,&c,sizeof(c));                               close(fd);                               //                               // Updates all widgets.                               //                               QWidgetList  *list = QApplication::allWidgets();                               QWidgetListIt it( *list );          // iterate over the widgets                               QWidget * w;                               while ( (w=it.current()) != 0 ) {   // for each widget...                                 ++it;                                 w->update();                               }                               delete list;                               // qApp->desktop()->repaint();                           }                         }                         break;          default: k=(-1); break;        }	if ( k >= 0 ) {		qwsServer->processKeyEvent( 0, k, 0, press, false );	}    }#endif}/* * vr41xx buttons driver */QWSVr41xxButtonsHandler::QWSVr41xxButtonsHandler() : QWSKeyboardHandler(){#ifdef QT_QWS_CASSIOPEIA    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;#endif}QWSVr41xxButtonsHandler::~QWSVr41xxButtonsHandler(){#ifdef QT_QWS_CASSIOPEIA    if ( buttonFD > 0 ) {	::close( buttonFD );	buttonFD = -1;    }    delete notifier;    notifier = 0;    delete [] kbdBuffer;#endif}void QWSVr41xxButtonsHandler::readKeyboardData(){#ifdef QT_QWS_CASSIOPEIA    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;	int keycode = Qt::Key_unknown;	switch ( (*code) & 0x0fff ) {	    case 0x7:		keycode = Qt::Key_Up;		break;	    case 0x9:		keycode = Qt::Key_Right;		break;	    case 0x8:		keycode = Qt::Key_Down;		break;	    case 0xa:		keycode = Qt::Key_Left;		break;	    case 0x3:		keycode = Qt::Key_Up;		break;	    case 0x4:		keycode = Qt::Key_Down;		break;	    case 0x1:		keycode = Qt::Key_Return;		break;	    case 0x2:		keycode = Qt::Key_F4;		break;	    default:		qDebug("Unrecognised key sequence %d", (int)code );	}	if ( (*code) & 0x8000 )	    processKeyEvent( 0, keycode, 0, FALSE, FALSE );	else	    processKeyEvent( 0, keycode, 0, TRUE, FALSE );/*	unsigned short t = *code;	for ( int i = 0; i < 16; i++ ) {	    keycode = (t & 0x8000) ? Qt::Key_1 : Qt::Key_0;	    int unicode = (t & 0x8000) ? '1' : '0';	    processKeyEvent( unicode, keycode, 0, TRUE, FALSE );	    processKeyEvent( unicode, keycode, 0, FALSE, FALSE );	    t <<= 1;	}	keycode = Qt::Key_Space;//	processKeyEvent( ' ', keycode, 0, TRUE, FALSE );//	processKeyEvent( ' ', keycode, 0, FALSE, FALSE );*/	idx += 2;    }    int surplus = kbdIdx - idx;    for ( int i = 0; i < surplus; i++ )	kbdBuffer[i] = kbdBuffer[idx+i];    kbdIdx = surplus;#endif}/* * Virtual framebuffer keyboard driver */#ifndef QT_NO_QWS_VFB#include "qvfbhdr.h"extern int qws_display_id;#endifQWSVFbKeyboardHandler::QWSVFbKeyboardHandler(){    kbdFD = -1;#ifndef QT_NO_QWS_VFB    kbdIdx = 0;    kbdBufferLen = sizeof( QVFbKeyData ) * 5;    kbdBuffer = new unsigned char [kbdBufferLen];    terminalName = QString(QT_VFB_KEYBOARD_PIPE).arg(qws_display_id);    if ((kbdFD = open( terminalName.local8Bit(), O_RDWR | O_NDELAY)) < 0) {	qDebug( "Cannot open %s (%s)", terminalName.latin1(),	strerror(errno));    } else {	// Clear pending input	char buf[2];	while (read(kbdFD, buf, 1) > 0) { }	notifier = new QSocketNotifier( kbdFD, QSocketNotifier::Read, this );	connect(notifier, SIGNAL(activated(int)),this, SLOT(readKeyboardData()));    }#endif}QWSVFbKeyboardHandler::~QWSVFbKeyboardHandler(){#ifndef QT_NO_QWS_VFB    if ( kbdFD >= 0 )	close( kbdFD );    delete [] kbdBuffer;#endif}void QWSVFbKeyboardHandler::readKeyboardData(){#ifndef QT_NO_QWS_VFB    int n;    do {	n  = read(kbdFD, kbdBuffer+kbdIdx, kbdBufferLen - kbdIdx );	if ( n > 0 )	    kbdIdx += n;    } while ( n > 0 );    int idx = 0;    while ( kbdIdx - idx >= (int)sizeof( QVFbKeyData ) ) {	QVFbKeyData *kd = (QVFbKeyData *)(kbdBuffer + idx);	if ( kd->unicode == 0 && kd->modifiers == 0 && kd->press ) {	    // magic exit key	    qWarning( "Instructed to quit by Virtual Keyboard" );	    qApp->quit();	}	processKeyEvent( kd->unicode&0xffff, kd->unicode>>16,				 kd->modifiers, kd->press, kd->repeat );	idx += sizeof( QVFbKeyData );    }    int surplus = kbdIdx - idx;    for ( int i = 0; i < surplus; i++ )	kbdBuffer[i] = kbdBuffer[idx+i];    kbdIdx = surplus;#endif}class QWSiMX1KeyboardHandler : public QWSKeyboardHandler{	Q_OBJECTpublic:	QWSiMX1KeyboardHandler();	virtual ~QWSiMX1KeyboardHandler(); private slots:	void readKeyboardData(); private:	unsigned char iMX1_keyVal;	QSocketNotifier *iMX1_notifier;	}; QWSiMX1KeyboardHandler::QWSiMX1KeyboardHandler()	: QWSKeyboardHandler(){	iMX1_notifier= 0;	kbdFD=open("/dev/PS2", O_NDELAY);	if (kbdFD>=0){		iMX1_notifier= new QSocketNotifier( kbdFD, QSocketNotifier::Read, this );		}	else{		qWarning("Cannot open /dev/i2c-keypad");	} 	connect( iMX1_notifier, SIGNAL(activated(int)),this,		 SLOT(readKeyboardData()) ); 	iMX1_keyVal= 0;} QWSiMX1KeyboardHandler::~QWSiMX1KeyboardHandler(){	if (kbdFD>=0){		::close(kbdFD);		kbdFD=-1;		delete iMX1_notifier;		iMX1_notifier=0;	}} void QWSiMX1KeyboardHandler::readKeyboardData(){	int keyCode= Qt::Key_unknown;		read(kbdFD, &iMX1_keyVal, 1); 	//qDebug("iMX1_keyVal=%d", int(iMX1_keyVal));		switch (iMX1_keyVal)	{		case 0x3c: keyCode= Qt::Key_Down;     qWarning("unrecognised iMX1_keyVal %x", iMX1_keyVal);  break;		case 0x1c: keyCode= Qt::Key_Return;  qWarning("unrecognised iMX1_keyVal %x", iMX1_keyVal);    break;                                                    		case 0x3f: keyCode= Qt::Key_Return;     qWarning("unrecognised iMX1_keyVal %x", iMX1_keyVal); break;		case 0x34: keyCode= Qt::Key_Up;    qWarning("unrecognised iMX1_keyVal %x", iMX1_keyVal); break;		case 0x27: keyCode= Qt::Key_F1;    qWarning("unrecognised iMX1_keyVal %x", int(iMX1_keyVal)); break;		case 0x56: keyCode= Qt::Key_F2;    qWarning("unrecognised iMX1_keyVal %d", int(iMX1_keyVal)); break;		case 255: break;                default:   break; 	       //	default: qWarning("unrecognised iMX1_keyVal %d", int(iMX1_keyVal));	}        if(iMX1_keyVal)        {           qWarning("unrecognised iMX1_keyVal %d", int(iMX1_keyVal));        }        iMX1_keyVal=0;	processKeyEvent( 0, keyCode, 0, true, false );}/* * keyboard driver instantiation */QWSKeyboardHandler *QWSServer::newKeyboardHandler( const QString &spec ){    QWSKeyboardHandler *handler = 0;    //handler= new QWSiMX1KeyboardHandler();    QString device;    QString type;    int colon=spec.find(':');    handler= new QWSiMX1KeyboardHandler();    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 {//	qWarning( "Keyboard type %s:%s unsupported", spec.latin1(), device.latin1() );     handler= new QWSiMX1KeyboardHandler();    }    return handler;}#include "qkeyboard_qws.moc"#endif // QNX6const QWSServer::KeyMap *QWSServer::keyMap(){    return keyM;}#endif // QT_NO_QWS_KEYBOARD

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -