在不使用X11的C中在GNU/Linux中捕获按键 [英] Capturing Keystrokes in GNU/Linux in C without X11
问题描述
如果我在一个应用程序中工作并且按键盘上的键,我如何在C中,在GNU/LINUX下,在用户区中,不使用X11的情况下捕获该键(或字符串),包括源应用程序的名称:)
If I am working in an application and I press key from keyboard, how can I capture that key (or string), including the source application's name, in C, under GNU/LINUX, in userland, without X11 :)
谢谢.
推荐答案
如果没有X11,这个问题会更加困难.
对于击键部分,您可以使用与此代码相似的代码,但是您必须将正在读取的设备(键盘,通常是/dev/input/event0)作为参数传递
Well, without X11 this problem is way more difficult.
For the keystroke part you can use a code similar to this one, but you have to pass as an argument the device that you are reading (keyboard, usually /dev/input/event0 )
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd;
if(argc < 2) {
printf("usage: %s <device>\n", argv[0]);
return 1;
}
fd = open(argv[1], O_RDONLY);
struct input_event ev;
while (1)
{
read(fd, &ev, sizeof(struct input_event));
if(ev.type == 1)
printf("key %i state %i\n", ev.code, ev.value);
}
}
信用不归我所有,此代码摘自Ventriloctrl黑客以获取击键. http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar .gz
Credits do not go to me, this code is taken from the Ventriloctrl hack to get keystrokes. http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz
希望我能帮上忙.
这篇关于在不使用X11的C中在GNU/Linux中捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!