我们可以在Linux中使用C ++使用原始ASCII值生成击键吗?

问题描述 投票:0回答:1

我有从条形码扫描仪收到的ASCII格式的条形码数据。我需要编写一个程序,以使用通过Linux中的条形码扫描仪接收到的数据模拟键盘。我也阅读了可用于Linux的USB-HIDKB驱动程序的源代码(http://lxr.free-electrons.com/source/drivers/hid/usbhid/usbkbd.c),但我觉得我必须做相反的事情。我想做的正是从扫描仪接收的数据作为ASCII格式的数据流,并且需要使用扫描的数据生成击键。数据读取部分即将完成,需要找到一种将ASCII数据转换为按键的方法。

示例操作:

有用于CTRL + z的条形码(undo操作的键盘快捷键),一旦扫描到条形码数据将被接收,08 10 03 00 1a 00 18 0b作为HEX中的数据被接收,则数据为1a 00 18 0b。在这里,前4个字节为标头,其余为数据部分。现在我需要做的是执行撤消操作,而不是打印数据。

欢迎任何代码示例或建议开始编码。谢谢。

c++ linux usb ascii keystroke
1个回答
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;
}

执行程序并切换到文本编辑器。您将看到在那里打印文本。

© www.soinside.com 2019 - 2024. All rights reserved.