[英]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.