[英]Qt c++ isolate keyboards
我正在Qt的一個關於樹莓派的項目。 我有一個usb鍵盤和一個usb磁卡讀卡器(讀作鍵盤)插入。我需要能夠隔離讀卡器輸入,以便它不能用於填充常規文本框並且讀取不同用於信用卡信息。 兩者似乎都將/ dev中的文件作為hidraw項目,盡管它們的排序是隨機的。 有沒有辦法可以通過編程方式將一個與另一個隔離? 在此先感謝您的幫助!
所以據我所知,沒有辦法使用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 = "";
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.