您好,欢迎访问三七文档
1、QT4.6widget工程项目2、在窗体上上放置一个label,命名为lbl_keyPad.3、widget.h中public:Widget(QWidget*parent=0);~Widget();添加voidkeyPressEvent(QKeyEvent*);4、widget.cpp中头文件添加#includeQKeyEvent内容添加//**********************keyboard键盘键值捕获*************************voidWidget::keyPressEvent(QKeyEvent*k){switch(k-key()){caseQt::Key_0:ui-lbl_keyPad-setText('0'Pressed!);break;caseQt::Key_1:ui-lbl_keyPad-setText('1'Pressed!);break;caseQt::Key_2:ui-lbl_keyPad-setText('2'Pressed!);break;caseQt::Key_3:ui-lbl_keyPad-setText('3'Pressed!);break;caseQt::Key_4:ui-lbl_keyPad-setText('4'Pressed!);break;caseQt::Key_5:ui-lbl_keyPad-setText('5'Pressed!);break;caseQt::Key_6:ui-lbl_keyPad-setText('6'Pressed!);break;caseQt::Key_7:ui-lbl_keyPad-setText('7'Pressed!);break;caseQt::Key_8:ui-lbl_keyPad-setText('8'Pressed!);break;caseQt::Key_9:ui-lbl_keyPad-setText('9'Pressed!);break;caseQt::Key_F1:ui-lbl_keyPad-setText('F1'Pressed!);break;caseQt::Key_F2:ui-lbl_keyPad-setText('F2'Pressed!);break;caseQt::Key_F3:ui-lbl_keyPad-setText('F3'Pressed!);break;caseQt::Key_F4:ui-lbl_keyPad-setText('F4'Pressed!);break;caseQt::Key_F5:ui-lbl_keyPad-setText('F5'Pressed!);break;caseQt::Key_F6:ui-lbl_keyPad-setText('F6'Pressed!);break;caseQt::Key_Asterisk:ui-lbl_keyPad-setText('*'Pressed!);break;caseQt::Key_Slash:ui-lbl_keyPad-setText('/'Pressed!);break;default:ui-lbl_keyPad-setText(OtherKeyPressed!);break;}}运行后,按下对应的键,窗体效果如下:
本文标题:QT键盘键值捕获
链接地址:https://www.777doc.com/doc-5011500 .html