qt 扫码枪扫描 输入事件 包含可输入字符

发布时间:2024年01月17日

原因说明

当需要对扫码枪扫描出的信息进行处理或打印等,有多个办法,其中串口,中间件等不在本文章讨论内。如果在网上搜索相对于解决办法,发现有大多数都是对输入事件进行处理,但在没有找到好的处理之后,作者自行进行了一些处理如下:

部分代码

首先是事件处理类

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所对应的字符编码,所以才有了以上的处理。

文章来源:https://blog.csdn.net/Sy_Faker/article/details/135652035
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。