Unix中的键盘设备 [英] Keyboard device in Unix

查看:141
本文介绍了Unix中的键盘设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获所有用户输入,一次读取一个字节?

How to capture all the user input, reading one byte at a time?

我做了这个编码,但是没用

I did this coding, but it's not working

if ((fd = open("/dev/char", O_RDONLY) != 1) {
    tty = open("/dev/tty", O_RDONLY);
    tcsetattr(0, TCSANOW, &orig_info);
    read (fd, &buf, sizeof(char));
}
close(fd);

我希望终端等待输入,但是没有.

I expected the terminal to wait for input, but it didn't.

推荐答案

键盘设备本身是/dev/input中的条目之一.您可以在/dev/input/by-path中按其连接类型(例如PS/2,USB等)找到键盘和其他输入设备.显然,您需要以root用户身份运行才能直接访问硬件,并且需要提供自己的转换,从键盘的原始字节到按键和释放键之类的东西.这可能不是您想要的.

The keyboard device itself is one of the entries in /dev/input. You can locate keyboards and other input devices by their connection type (e.g. PS/2, USB, …) in /dev/input/by-path. Obviously, you'll need to run as root to access the hardware directly, and you'll need to provide your own translation from raw bytes coming from the keyboard into things like key presses and key releases. This is probably not what you want.

如果您正在运行GUI应用程序,则低级方法是调用

If you're running a GUI application, the low-level method is to call XNextEvent and other functions in the same family. Decoding input events isn't completely trivial, as it's up to applications to apply modifiers. A GUI framework (Motif, Gtk, Qt, …) would help you.

如果正在运行终端应用程序,请从标准输入或/dev/tty中读取(/dev/tty始终是程序运行所在的终端,即使已重定向标准输入).您需要将终端置于原始模式.您将获得解码后的字符键,而功能键通常以转义序列的形式出现.在这里,图书馆也有帮助.事实标准是 ncurses .

If you're running a terminal application, read from standard input or from /dev/tty (/dev/tty is always the terminal that your program is running on, even if standard input has been redirected). You'll want to put the terminal in raw mode. You'll get decoded character keys, and function keys mostly as escape sequences. Here, too, a library helps; the de facto standard is ncurses.

这篇关于Unix中的键盘设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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