简体   繁体   English

有没有办法检查 /dev/input/ 中的哪个事件文件用于键盘输入?

[英]Is there a way to check which event file in /dev/input/ is being used for keyboard input?

Basically, what I am trying to do is capture keyboard input from the user, using files in "/dev/input/".基本上,我想做的是使用“/dev/input/”中的文件从用户那里捕获键盘输入。 However, a problem I have come across is that the file which handles keyboard input is different on event files.但是,我遇到的一个问题是处理键盘输入的文件在事件文件上是不同的。 What do I mean by this?我这是什么意思? Well, on my specific machine, "/dev/input/event5" works with this program, but I've seen that, on some people's machines, the file might be event4, event0, event3, or even event17.好吧,在我的特定机器上,“/dev/input/event5”可以与这个程序一起使用,但我已经看到,在某些人的机器上,该文件可能是 event4、event0、event3,甚至是 event17。

Is there a way that I can programmatically figure out which file handles keyboard input?有没有一种方法可以让我以编程方式找出处理键盘输入的文件?

If you want to test the code, make sure you run with root privileges.如果要测试代码,请确保以 root 权限运行。 You need them to open any files in "/dev/input/".您需要它们打开“/dev/input/”中的任何文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/input.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
 
#define UK "UNKNOWN"
#define ESCAPE(key) (key == KEY_ESC)
#define SHIFT(key)  ((key == KEY_LEFTSHIFT) || (key == KEY_RIGHTSHIFT))
 
static const char *keycodes[] =
{
    "RESERVED", "ESC", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    "-", "=", "BACKSPACE", "TAB", "q", "w", "e", "r", "t", "y", "u", "i",
    "o", "p", "[", "]", "ENTER", "L_CTRL", "a", "s", "d", "f", "g", "h",
    "j", "k", "l", ";", "'", "`", "L_SHIFT", "\\", "z", "x", "c", "v", "b",
    "n", "m", ",", ".", "/", "R_SHIFT", "*", "L_ALT", "SPACE", "CAPS_LOCK", 
    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "NUM_LOCK",
    "SCROLL_LOCK", "NL_7", "NL_8", "NL_9", "-", "NL_4", "NL5",
    "NL_6", "+", "NL_1", "NL_2", "NL_3", "INS", "DEL", UK, UK, UK,
    "F11", "F12", UK, UK,   UK, UK, UK, UK, UK, "R_ENTER", "R_CTRL", "/", 
    "PRT_SCR", "R_ALT", UK, "HOME", "UP", "PAGE_UP", "LEFT", "RIGHT", "END", 
    "DOWN", "PAGE_DOWN", "INSERT", "DELETE", UK, UK, UK, UK,UK, UK, UK, 
    "PAUSE"
};
 
static const char *shifted_keycodes[] =
{
    "RESERVED", "ESC", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", 
    "_", "+", "BACKSPACE", "TAB", "Q", "W", "E", "R", "T", "Y", "U", "I", 
    "O", "P", "{", "}", "ENTER", "L_CTRL", "A", "S", "D", "F", "G", "H", 
    "J", "K", "L", ":", "\"", "~", "L_SHIFT", "|", "Z", "X", "C", "V", "B", 
    "N", "M", "<", ">", "?", "R_SHIFT", "*", "L_ALT", "SPACE", "CAPS_LOCK", 
    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "NUM_LOCK", 
    "SCROLL_LOCK", "HOME", "UP", "PGUP", "-", "LEFT", "NL_5", 
    "R_ARROW", "+", "END", "DOWN", "PGDN", "INS", "DEL", UK, UK, UK, 
    "F11", "F12", UK, UK,   UK, UK, UK, UK, UK, "R_ENTER", "R_CTRL", "/", 
    "PRT_SCR", "R_ALT", UK, "HOME", "UP", "PAGE_UP", "LEFT", "RIGHT", "END", 
    "DOWN", "PAGE_DOWN", "INSERT", "DELETE", UK, UK, UK, UK,UK, UK, UK, 
    "PAUSE"
};
 
static int running;
static int keyboard_fd;
 
static void sig_handler(int signo)
{
    running = 0;
}
 
void input_demo_init(char *keyboard_eventfile)
{
    signal(SIGINT, sig_handler);
 
    running = 1;
 
    if ((keyboard_fd = open(keyboard_eventfile, O_RDONLY)) < 0) {
        fprintf(stderr, "\nUnable to read from the device\n");
        exit(EXIT_FAILURE);
    }
}
 
void input_demo_exit(void)
{
    close(keyboard_fd);
}
 
void input_demo_run(void)
{
    int shift_flag = 0;
    struct input_event event;
 
    while (running) {
        read(keyboard_fd, &event, sizeof(event));
 
        /* If a key from the keyboard is pressed */
        if (event.type == EV_KEY && event.value == 1) {
            if (ESCAPE(event.code))
                return;
 
            if (SHIFT(event.code))
                shift_flag = event.code;
 
            if (shift_flag && !SHIFT(event.code))
                printf("%s\n", shifted_keycodes[event.code]);
            
            else if (!shift_flag && !SHIFT(event.code))
                printf("%s\n", keycodes[event.code]);
        }
        else {
            /* If a key from the keyboard is released */
            if (event.type == EV_KEY && event.value == 0)
                if (SHIFT(event.code))
                    shift_flag = 0;
        }
    }
}
 
int main(int argc, char **argv) {
 
    input_demo_init("/dev/input/event5");
    input_demo_run();
    input_demo_exit();
 
    return 0;
}

You can try你可以试试

cat /proc/bus/input/devices
I: Bus=001e Vendor=0000 Product=0000 Version=0001
N: Name="vc4"
P: Phys=vc4/input0
S: Sysfs=/devices/platform/soc/fef00700.hdmi/rc/rc0/input1
U: Uniq=
H: Handlers=kbd event0
B: PROP=20
B: EV=100017
B: KEY=ffffc000000000 3ff 0 400000320fc200 40830c900000000 0 210300 49d2c040ec00 1e378000000000 8010000010000000
B: REL=3
B: MSC=10

I: Bus=001e Vendor=0000 Product=0000 Version=0001
N: Name="vc4"
P: Phys=vc4/input0
S: Sysfs=/devices/platform/soc/fef05700.hdmi/rc/rc1/input2
U: Uniq=
H: Handlers=kbd event1
B: PROP=20
B: EV=100017
B: KEY=ffffc000000000 3ff 0 400000320fc200 40830c900000000 0 210300 49d2c040ec00 1e378000000000 8010000010000000
B: REL=3
B: MSC=10

hope that can help you.希望可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 /dev/input/eventx 读取原始键盘事件 - cannot read raw keyboard event from /dev/input/eventx 从/ dev / input / event读取 - Read from /dev/input/event Linux键盘原始读取,什么更好,从/ dev / input / event0读取或从stdin读取? - Linux keyboard raw reading, what's better, reading from /dev/input/event0 or reading from stdin? 除了/ dev / input / eventx之外,是否有基于更高级别(字符)的访问Linux键盘的方法? - is there a higher level (character?) based way to access a Linux keyboard other than /dev/input/eventx? 在 android 如何使用 c function 找到用于触摸屏的 /dev/input/event* - In android how to find what /dev/input/event* is used for touchscreen using c function 为什么即使使用fprintf将输入传递到文件指针,输入也不会写入文件? - Why is the input not being written to file even if fprintf is used to pass the input to the file pointer? 从 /dev/tty 读取时,输入和键盘缓冲区中发生了什么? - When reading from /dev/tty, what is happening in input and keyboard buffer? inotify事件后无法打开/ dev / input / js文件描述符 - Can't open /dev/input/js file descriptor after inotify event 为什么在使用或不使用 libevdev 的情况下轮询和读取 /dev/input/event 文件描述符时事件会重复 - why are events duplicated when polling and reading /dev/input/event file descriptors with or without libevdev C:如何在一次使用scanf的同时检查键盘或文件输入的结束 - C : How to check end of input from keyboard or file while using scanf one char at a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM