我从ASCII格式的条形码扫描仪接收条形码数据。我需要编写一个程序,使用通过Linux中的条形码扫描仪接收到计算机的数据来模拟键盘。我也阅读了适用于Linux的USB-HIDKB驱动程序的源代码(http://lxr.free-electrons.com/source/drivers/hid/usbhid/usbkbd.c),但我觉得我必须反过来。 我想要做的就是数据从扫描仪接收为ASCII格式的数据流,需要使用扫描数据生成击键。数据读取部分已基本完成,需要找到一种将ASCII数据转换为击键的方法。
示例操作:
ctr + z
有一个条形码(undo
操作的键盘快捷键),一旦扫描条形码数据将收到,接收到08 10 03 00 1a 00 18 0b
作为HEX中的数据,然后数据为{ {1}}。这里前4个字节是标题,剩下的是数据部分。
现在我需要做的是执行撤销操作而不是打印数据。
我欢迎任何代码示例或建议开始编码。感谢。
答案 0 :(得分:0)
找到了一段有用的代码 [1]: http://www.doctort.org/adam/nerd-notes/x11-fake-keypress-event.html ,帮助我开始使用已处理的数据来模拟击键。
但这仅适用于具有xwindowing系统的系统。这不适用于仅基于终端的系统。在这种情况下,我们需要使用uinput子系统来生成软件按键。
关于如何在Linux中使用uinput的示例程序可以在http://thiemonge.org/getting-started-with-uinput
找到我使用uinput编写了一个简单的程序。这很有效。
示例代码:
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int SetupUinputDevice()
{
// file descriptor for input subsystem device
int fd_uinput_device;
// read only, non-blocking, no delay file descriptor
fd_uinput_device = open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_NDELAY);
if(fd_uinput_device < 0)
{
std::cout << "Error : open file descriptor /dev/uinput : " << strerror(errno) << std::endl;
return -1;
}
// create and initiate uinput_user_dev struct for KeyboardEmulator device
struct uinput_user_dev dev_key_board_emulator;
memset(&dev_key_board_emulator, 0, sizeof(uinput_user_dev));
snprintf(dev_key_board_emulator.name, UINPUT_MAX_NAME_SIZE, "zebra-scanner-hidkb-emulator");
dev_key_board_emulator.id.bustype = BUS_USB;
dev_key_board_emulator.id.vendor = 0x01;
dev_key_board_emulator.id.product = 0x02;
dev_key_board_emulator.id.version = 1;
/** configure the uinput device to key board events, these will inform to
* subsystem via ioctl calls which type of events will be handled by
* the device
*/
// configure/set key press and release events
if(ioctl(fd_uinput_device, UI_SET_EVBIT, EV_KEY) < 0)
{
std::cout << "Error : ioctl : UI_SET_EVBIT for EV_KEY " << strerror(errno) << std::endl;
return -1;
}
// enable set of key board events
for(int iEvent=0; iEvent < 254; iEvent++)
{
if(ioctl(fd_uinput_device, UI_SET_KEYBIT, iEvent) < 0)
{
std::cout << "Error : ioctl : UI_SET_KEYBIT for event ID: " << iEvent << " : " << strerror(errno) << std::endl;
}
}
// enable synchronization events
if(ioctl(fd_uinput_device, UI_SET_EVBIT, EV_SYN) < 0)
{
std::cout << "Error : ioctl : UI_SET_EVBIT for EV_SYN: " << strerror(errno) << std::endl;
return -1;
}
// write the uinput_user_dev structure into the device file descriptor
if(write(fd_uinput_device, &dev_key_board_emulator, sizeof(uinput_user_dev)) < 0)
{
std::cout << "Error : failed to write uinput_user_dev structure into the device file descriptor: " << strerror(errno) << std::endl;
return -1;
}
// Create the end point file descriptor for user input device descriptor.
if(ioctl(fd_uinput_device, UI_DEV_CREATE) < 0)
{
std::cout << "Error : failed to create end point for user input device: " << strerror(errno) << std::endl;
return -1;
}
return fd_uinput_device;
}
int CloseUinputDevice(int fd_dev)
{
if(ioctl(fd_dev, UI_DEV_DESTROY) < 0)
{
std::cout << "Error : ioctl failed: UI_DEV_DESTROY : " << strerror(errno) << std::endl;
return -1;
}
if(close(fd_dev) < 0)
{
std::cout << "Error : close device file descriptor : " << strerror(errno) << std::endl;
return -1;
}
}
int SendKeyboardEvents(int fd_dev, int event_type, int key_code, int value)
{
// input_event struct member for input events
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(input_event));
// set event values
key_input_event.type = event_type;
key_input_event.code = key_code;
key_input_event.value = value;
if(write(fd_dev, &key_input_event, sizeof(input_event)) < 0)
{
std::cout << "Error writing input events to the device descriptor: " << strerror(errno) << std::endl;
return -1;
}
return 0;
}
int main(int argc, char** argv) {
std::cout << "------------key - emulator------------" << std::endl;
int fd_keyEmulator = SetupUinputDevice();
sleep(3);
if(fd_keyEmulator < 0)
{
std::cout << "Error in setup file descriptor for uinput device..." << std::endl;
return 0;
}
// start to send events
int returnValue;
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_A, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_A, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_B, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_B, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_C, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_C, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_D, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_D, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_E, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_E, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_F, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_F, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_G, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_G, 0);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_H, 1);
returnValue = SendKeyboardEvents(fd_keyEmulator, EV_KEY, KEY_H, 0);
CloseUinputDevice(fd_keyEmulator);
std::cout << "------------end of program------------" << std::endl;
return 0;
}
执行程序并切换到文本编辑器。你会看到在那里打印文字。