用C捕获键击在GNU / Linux的 [英] Capturing Keystrokes in GNU/Linux in C

查看:123
本文介绍了用C捕获键击在GNU / Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在应用程序中我的工作和我美元,键盘p $ PSS关键,我怎么能捕获键(或字符串),包括源应用程序的名称,在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这个问题的方式更加困难。结果
对于按键部分,你可以使用类似于这一个code,但你必须作为参数传递您正在阅读设备(键盘,通常为/ dev /输入/ 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);

    }
}

学分不要去我来说,这code从Ventriloctrl采取黑客获得按键。
<一href=\"http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz\">http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz

希望我的一些帮助。

这篇关于用C捕获键击在GNU / Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆