我正在 Qt 的树莓派上做一个项目。我有一个 USB 键盘和一个 USB 磁卡读卡器(读取为键盘)插入。我需要能够隔离读卡器输入,以便它不能用于填充常规文本框并且以不同方式读取信用卡信息。两者似乎都将它们的文件作为 hidraw 项目放在 /dev 中,尽管它们的顺序是随机的。有没有一种方法可以以编程方式将一个与另一个隔离?提前感谢您的帮助!
问问题
450 次
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 = "";
}
}
}
}
}
于 2017-02-20T07:10:32.920 回答