gpt4 book ai didi

c++ - Qt C++ 隔离键盘

转载 作者:太空狗 更新时间:2023-10-29 23:13:02 24 4
gpt4 key购买 nike

我正在用 Qt 开发一个关于树莓派的项目。我插入了一个 USB 键盘和一个 USB 磁卡读卡器(读取为键盘)。我需要能够隔离读卡器输入,以便它不能用于填充常规文本框并且读取方式不同信用卡信息。两者似乎都将它们的文件作为 hidraw 项目放在/dev 中,尽管它们的顺序是随机的。有没有办法以编程方式将一个与另一个隔离开来?在此先感谢您的帮助!

最佳答案

据我所知,没有办法使用 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 = "";
}
}
}
}
}

关于c++ - Qt C++ 隔离键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com