linux input读取键盘,Linux键盘原始阅读,有什么更好的,从/ dev / input / event0读取或从标准输入读取?(Linux keyboard raw reading, wh...

Linux键盘原始阅读,有什么更好的,从/ dev / input / event0读取或从标准输入读取?(Linux keyboard raw reading, what's better, reading from /dev/input/event0 or reading from stdin?)

我正在为Raspberry Pi制作一个小型C视频游戏库。 我从头开始对输入系统进行编码,在阅读了一些关于原始输入阅读的示例之后,我有些疑惑。

对于鼠标阅读,我只是使用/ dev / input / event1,我打开()它作为O_NONBLOCK,我读()input_event(s),我也把鼠标读取在一个单独的pthread。 简单。

对于键盘阅读,我看到一些例子,重新配置标准输入到O_NONBLOCK(使用fcntl()),然后保存并重新配置键盘termios attibutes(ICANON,ECHO),一些例子还保存并重新配置键盘模式与ioctl()。 做所有这些事情的重点是什么,而不是只读/ dev / input / event0 input_event(s)(与鼠标相同)?

请注意,我不知道这些函数做什么,我只是不明白为什么应该更好地做所有的事情,而不是仅仅读取input_event(s)。

I'm working on a small C videogames library for the Raspberry Pi. I'm coding the input system from scratch and after reading and seeing some examples about raw input reading, I got some doubts.

For mouse reading, I just use /dev/input/event1, I open() it as O_NONBLOCK, I read() input_event(s) and I also put the mouse reading in a separate pthread. Easy.

For keyboard reading, I saw some examples that reconfigure stdin to O_NONBLOCK (using fcntl()), then save and reconfigure keyboard termios attibutes (ICANON, ECHO), and some example also save and reconfigure keyboard mode with ioctl(). What's the point of doing all that stuff instead of just reading /dev/input/event0 input_event(s) (same way as mouse)?

Note that I undestand what those functions do, I just don't undestand why should be better to do all that stuff instead of just reading input_event(s).

原文:https://stackoverflow.com/questions/25557027

更新时间:2019-09-26 00:31

最满意答案

读取标准输入不限于读取本地连接的键盘(但有其他限制,使其大多不适合游戏)。 然后读取标准输入,您可以使用本地连接的串行终端或终端仿真程序(可能从远程X服务器运行)读取远程登录用户的击键。

对于基于终端的游戏,使用stdin可能有意义。 但是,使用GPM而不是读取鼠标的/ dev / input / event1可能会更好。 甚至可能更好地使用ncurses的一切。

否则,您可能需要查看SDL ,如果不是直接使用它,至少要了解读取输入的不同方法。 例如,SDL全面支持使用X的网络透明度。这意味着游戏可以在一台计算机上执行,但可以在另一台计算机上执行。

Reading stdin is not limited to reading locally connected keyboards (but has other limitation making it mostly unsuitable for games). Then reading stdin you could be reading keystrokes from a user logged in remotely, using a locally connected serial terminal or a terminal emulator (possibly operated from a remote X server).

For terminal based games it might make sense to use stdin. But then it would probably be better to use GPM instead of reading /dev/input/event1 for the mouse. And possibly even better to use ncurses for everything.

Otherwise you might want to look at SDL, if not for using it directly, at least for learning about different ways to read input. For example, SDL has full support for network transparency using X. Which means a game can execute on one computer but be played on another.

2014-08-28

相关问答

urandom是通过设备驱动程序提供的,内核对驱动程序的第一件事就是调用init调用。 如果你看看这里: http : //lxr.free-electrons.com/source/drivers/char/random.c#L1401 * Note that setup_arch() may call add_device_randomness()

* long before we get here. This allows seeding of the pools

* with

...

与Windows不同,Linux不会因为某些其他进程打开文件而锁定文件。 我刚刚在我的HTC Desire上打开了一个shell,看看: $ ls -l /dev/input

crw-rw---- root input 13, 73 2012-03-02 19:19 event9

crw-rw---- root input 13, 72 2012-03-02 19:19 event8

(etc)

如您所见,这些文件只能由用户“root”和组“input”读写。

...

读取标准输入不限于读取本地连接的键盘(但有其他限制,使其大多不适合游戏)。 然后读取标准输入,您可以使用本地连接的串行终端或终端仿真程序(可能从远程X服务器运行)读取远程登录用户的击键。 对于基于终端的游戏,使用stdin可能有意义。 但是,使用GPM而不是读取鼠标的/ dev / input / event1可能会更好。 甚至可能更好地使用ncurses的一切。 否则,您可能需要查看SDL ,如果不是直接使用它,至少要了解读取输入的不同方法。 例如,SDL全面支持使用X的网络透明度。这意味着游戏

...

不,我不认为有一种方法可以使用命令行执行此操作。 但您可以使用JFrame并向其添加CaretListener 。 No I don't think that there is a way to do this with the command-line. But instead you could use a JFrame and add a CaretListener to it.

基于@Joni的建议,我把它放在一起: Scanner scanner = new Scanner(System.in);

int choice = 0;

while (scanner.hasNext()){

if (scanner.hasNextInt()){

choice = scanner.nextInt();

break;

} else {

scanner.next(); // Just discard this, not i

...

shell命令走错了方向,例如它应该是这样的 ./a.out < /dev/input/event0 > /dev/tty0

但是,在应用程序中使用newterm可以获得更好的结果(并直接打开设备),而不是依靠通过shell传递流并使用initscr 。 The shell command is going in the wrong direction, e.g., it should be something like ./a.out < /dev/input/event0 > /dev/tt

...

模块readline-sync很好地完成了这项工作。 npm install readline-sync

接着: var readlineSync = require('readline-sync');

var answer = readlineSync.question('What is your favorite food? :');

console.log('Oh, so your favorite food is ' + answer);

https://www.npmjs.com/p

...

在您向sys.stderr打印'Not prime'之后,您调用sys.exit会导致程序终止。 如果删除它,则应继续读取sys.stdin直到发出EOF字符(Windows上为Ctrl + Z,POSIX上为Ctrl + D)。 Well after you print 'Not prime' to sys.stderr you make a call to sys.exit which causes your program to terminate. If you remove this,

...

在我的系统上, event2代表鼠标,以下简单的readloop程序就像魅力一样。 以root身份运行 : #include

#include

#include

#include // for debug output

#include

#include // f

...

您可能正在中止仍然连接到标准输入的线程,请记住您在执行getline时中止了线程。 现在我不知道是否有办法获取标准输入指针并释放它以便能够获得更多行但是可能有一种方法可以继续读取前一个线程已读取的行。 你需要做的是: 进入getline时使用共享标志并将其设置为true 将您读取的变量设置为静态变量,并且除了需要重新启动的线程之外,不要在任何线程中使用getline 当你重建新线程时,如果输入getline的标志为真,则不要调用getline 完成行重置布尔标志后 使用锁定来防止读取线标志上的并发

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值