当需要对扫码枪扫描出的信息进行处理或打印等,有多个办法,其中串口,中间件等不在本文章讨论内。如果在网上搜索相对于解决办法,发现有大多数都是对输入事件进行处理,但在没有找到好的处理之后,作者自行进行了一些处理如下:
首先是事件处理类
class GlobalApplication : public QApplication, public QAbstractNativeEventFilter
{
public:
GlobalApplication(int &argc,char **argv);
~GlobalApplication();
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override;
//此为重构的处理函数
};
然后是实现重构代码
bool GlobalApplication::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
if(eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG")
{
//MSG* pMSG =(MSG*)message;
//这里是对接收到的数据message进行处理,根据各自项目不同
/*if(pMSG->message == WM_INPUT)
{
unsigned short thisOne = raw->data.keyboard.VKey;
handleScanCode(thisOne, curTime);
//总之重点是在对信息的处理这函数里
}*/
}
}
最后是信息处理函数
void handleScanCode(unsigned short thisOne){
//--------------------------------字母----------------------------------------------------
if(thisOne >= 65 && thisOne <= 90){
if (!isNextCodeGreater) {
thisOne = thisOne+32;
isNextCodeGreater = false;
}
curScanCode+=QChar(thisOne);
}
//---------------------------------------------------------------------------------------
//----------------------------------特殊字符与数字------------------------------------------
if (thisOne == 189) {//_-
if (isNextCodeGreater) {
thisOne = 95;
} else {
thisOne = 45;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 187) {//+=
if (isNextCodeGreater) {
thisOne = 43;
} else {
thisOne = 61;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 219) {//{[
if (isNextCodeGreater) {
thisOne = 123;
} else {
thisOne = 91;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 221) {//}]
if (isNextCodeGreater) {
thisOne = 125;
} else {
thisOne = 93;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 188) {//<,
if (isNextCodeGreater) {
thisOne = 60;
} else {
thisOne = 44;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 190) {//>.
if (isNextCodeGreater) {
thisOne = 62;
} else {
thisOne = 46;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 191) {//?/
if (isNextCodeGreater) {
thisOne = 63;
} else {
thisOne = 47;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 186) {//:;
if (isNextCodeGreater) {
thisOne = 58;
} else {
thisOne = 59;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 222) {//"'
if (isNextCodeGreater) {
thisOne = 34;
} else {
thisOne = 39;
}
curScanCode+=QChar(thisOne);
}
if (thisOne == 220) {// |
if (isNextCodeGreater) {
thisOne = 124;
} else {
thisOne = 92;
}
curScanCode+=QChar(thisOne);
}
if (thisOne >= 48 && thisOne <= 57) {
if (isNextCodeGreater) {
map<unsigned short, unsigned short> numberToSymbol = {{48,41}, {49,33}, {50,64}, {51,35}, {52,36},
{53,37}, {54,94}, {55,38}, {56,42}, {57,40}};
thisOne = numberToSymbol[thisOne];
}
curScanCode+=QChar(thisOne);
}
//---------------------------------------------------------------------------------------
if (thisOne == 16) {
isNextCodeGreater = true;
} else {
isNextCodeGreater = false;
}
}
与大多数键盘输入相同,上档即shift键会对键盘输入的内容产生影响,在qt的事件中也是如此,比如直接按键盘上的q就是q所对应的字符编码,如果信息是Q那么在qt的事件则是先给一个16,之后再是q所对应的字符编码,所以才有了以上的处理。