我正在使用Qt在树莓派上进行项目开发。我连接了一个USB键盘和一个USB磁卡读卡器(作为键盘读取)。我需要能够隔离磁卡读卡器输入,以便它不能用于填充常规文本框,并且可以按照不同的方式读取信用卡信息。虽然它们的顺序是随机的,但两者似乎都在/dev中有它们自己的hidraw文件。是否有方法可以通过编程将它们隔离开来?非常感谢你的帮助!
我正在使用Qt在树莓派上进行项目开发。我连接了一个USB键盘和一个USB磁卡读卡器(作为键盘读取)。我需要能够隔离磁卡读卡器输入,以便它不能用于填充常规文本框,并且可以按照不同的方式读取信用卡信息。虽然它们的顺序是随机的,但两者似乎都在/dev中有它们自己的hidraw文件。是否有方法可以通过编程将它们隔离开来?非常感谢你的帮助!
#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 = "";
}
}
}
}
}
evrouter
。它可以根据设备名称过滤输入事件。(如果您使用完整的 X 环境) - Velkanevdev
。我看到你可以传递一个环境变量来设置使用哪个设备。或者EGLFS使用libinput
。无论哪种方式,都有命令行工具在evdev
和libinput
中来查找设备的名称并读取其输入。有一个名为QT_LOGGING_RULES=qt.qpa.input=true
的变量,可以查看Qt使用了什么。 - Velkan