Qt C++隔离键盘

4

我正在使用Qt在树莓派上进行项目开发。我连接了一个USB键盘和一个USB磁卡读卡器(作为键盘读取)。我需要能够隔离磁卡读卡器输入,以便它不能用于填充常规文本框,并且可以按照不同的方式读取信用卡信息。虽然它们的顺序是随机的,但两者似乎都在/dev中有它们自己的hidraw文件。是否有方法可以通过编程将它们隔离开来?非常感谢你的帮助!


尝试使用 evrouter。它可以根据设备名称过滤输入事件。(如果您使用完整的 X 环境) - Velkan
糟糕,我没有处于X环境中,因为我正在使用EGLFS。不过还是谢谢! - Nick
1
也许它仍然使用evdev。我看到你可以传递一个环境变量来设置使用哪个设备。或者EGLFS使用libinput。无论哪种方式,都有命令行工具在evdevlibinput中来查找设备的名称并读取其输入。有一个名为QT_LOGGING_RULES=qt.qpa.input=true的变量,可以查看Qt使用了什么。 - Velkan
1个回答

1
据我所知,无法使用Qt来确定事件的来源。不幸的是,也没有办法使用udev更改内核节点,因此无法阻止Qt使用其输入文件。我所能做的唯一一件事就是获取输入文件并获得独占访问权,从而排除Qt。我在一个单独的线程中执行此操作,等待设备输入,然后使用信号报告它。对于那些感兴趣的人,我将发布一些QThread代码片段。
#include <QObject>
#include <QThread>
#include <QDebug>
#include <qplatformdefs.h>
#include "stdio.h"
#include "constants.h"
#include "linux/input.h"    

namespace KeyboardConstants {
        static const QString keys[] = {"","","1","2","3","4","5","6","7","8","9","0",
                                       "-","=","","","q","w","e","r","t","y","u",
                                      "i","o","p","[","]","","","a","s","d","f",
                                      "g","h","j","k","l",";","'","`","","\\","z",
                                      "x","c","v","B","n","m",",",".","/","","","",
                                      " ","",""};
        static const QString shiftKeys[] = {"","","!","@","#","$","%","^","&","*","(",")",
                                           "_","+","","","Q","W","E","R","T","Y","U",
                                           "I","O","P","{","}","","","A","S","D","F",
                                           "G","H","J","K","L",":","\"","~","","|","Z",
                                           "X","C","V","B","N","M","<",">","?","","",""};
    }

QString input = "";

void ccWatcher::run()
{
    struct input_event ev[1];
    int fevdev = -1;
    int size = sizeof(struct input_event);
    int rd;
    char name[256] = "Unknown";
    bool shift = false;
    QString device = "/dev/input/by-id/usb-XXXX";


    fevdev = open(device.toStdString().c_str(), O_RDONLY);

    if (fevdev >= 0) {
        ioctl(fevdev, EVIOCGNAME(sizeof(name)), name);
        // Gain exclusive access to the input_event file
        ioctl(fevdev, EVIOCGRAB, 1);
        while (1)
        {
            // Shouldn't happen, but you never know
            if ((rd = read(fevdev, ev, size)) < size) {
                break;
            }
            // Make sure the type is "key" and the value is 1
            if (ev[0].type == 1 && ev[0].value == 1) {
                // 28 and 96 are the codes for 'enter'
                if (ev[0].code != 28 && ev[0].code != 96) {
                    // 42 and 54 are the codes for shift
                    if (ev[0].code == 42 || ev[0].code == 54) {
                        shift = true;
                    } else {
                        if (shift) {
                            input.append(KeyboardConstants::shiftKeys[ev[0].code]);
                            shift = false;
                        } else input.append(KeyboardConstants::keys[ev[0].code]);
                    }
                } else {
                    emit ccReadin(input);
                    input = "";
                }
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接