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

原因说明

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

部分代码

首先是事件处理类

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

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你的扫码是通过 USB 接口连接到计算机上的,那么你可以使用 Qt 的 HID API 对其进行读取。以下是一个简单的示例代码: ```cpp #include <QtGui> #include <QtHid> class ScannerReader : public QObject { Q_OBJECT public: ScannerReader(QObject *parent = nullptr) : QObject(parent) { // 找到设备并打开连接 QList<QHidDeviceInfo> devices = QHidDeviceInfo::enumerate(0x0, 0x0); if (!devices.isEmpty()) { QHidDevice *device = new QHidDevice(devices.first(), this); if (device->open(QIODevice::ReadWrite)) { connect(device, &QHidDevice::readyRead, this, &ScannerReader::readData); } } } signals: void codeRead(QString code); private slots: void readData() { QHidDevice *device = qobject_cast<QHidDevice *>(sender()); if (device) { QByteArray data = device->readAll(); QString code = QString::fromLocal8Bit(data); emit codeRead(code); } } }; ``` 在上面的示例代码中,我们创建了一个名为 `ScannerReader` 的自定义类,用于读取扫码数据。在类的构造函数中,我们使用 `QHidDeviceInfo::enumerate()` 方法查找与扫码匹配的设备,并打开连接。然后,我们使用 `connect()` 方法将设备的 `readyRead` 信号连接到 `readData()` 槽函数中。 在 `readData()` 槽函数中,我们读取设备的数据并将其转换为字符串格式,然后将其发出 `codeRead` 信号,以便在其他地方处理扫码读取到的数据。 你可以在其他地方使用如下代码来实例化 `ScannerReader` 类并接收扫码的数据: ```cpp ScannerReader reader; connect(&reader, &ScannerReader::codeRead, [](QString code){ qDebug() << "Read code:" << code; }); ``` 这里我们使用 `connect()` 方法将 `codeRead` 信号连接到一个 lambda 函数中,在函数中输出扫码读取到的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值