如何在Linux中立即捕获击键? [英] How can I capture a key stroke immediately in linux?

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

问题描述

可能重复:
等同于getch()& Linux中的getche()?

Possible Duplicate:
What is equivalent to getch() & getche() in Linux?

我是linux编程的新手. :-)

I'm a newbie in linux programming. :-)

我只是想知道我的程序(使用 C 语言)是否可以立即捕获linux中的每个按键,然后决定是否回显它以及如何显示它,就像telnet一样.

I just wonder if my program (using C language) can capture every single key stroke immediately in linux, and then decide whether to echo it or not and how to display it, just like telnet.

例如,如果用户点击"A",我希望程序显示"B",如果他键入"B",则需要"C",等等.

for example, if a user strikes 'A', I want the program to display 'B', if he types 'B', 'C' is wanted, and so on.

听起来很有趣而且没用.我对此很好奇.

It sounds funny and useless. I am just curious about it.

推荐答案

基本上,它很大程度上取决于您如何立即定义.

Basically, it depends heavily on how you define immediately.

这里有两个任务.第一种是禁用大多数C输入库中内置的常规键回显.第二种是打印出新字符,而不是旧字符.

There are two tasks here. The first is to disable the regular key echoing that is built into most C input libraries. The second is to print out the new character instead of the old one.

使用伪代码.

 echo(off);
 while (capturing && charIsAvailable()) {
   c = readOneChar();
   if (c == '\n') {
     capturing = false;
   }
   printf("%c", c++);
 }
 echo(on);

有许多通信系统可以捕获按键.

There are a number of systems communicating to capture a key press.

  1. 键盘
  2. (可能是)USB总线
  3. CPU中断处理程序
  4. 操作系统
  5. X窗口服务器进程
  6. 具有焦点的X窗口".

最后一步是通过运行连续循环的程序完成的,该循环从X服务器捕获事件并进行处理.如果您想以某些方式扩展此程序(获取按下键的时间),则需要告诉其他程序您想要原始"键盘事件,这意味着您实际上不会完全接受煮熟的" "人物.结果,您将必须跟踪哪些键向上和向下以及持续多长时间,并处理程序中所有奇数元键的行为(这不是'a'而是'A',因为 shift 已关闭等).

The last step is done with a program that runs a continuous loop that captures events from the X server and processes them. If you wanted to expand this program in certain ways (get the length of time the key was pressed) you need to tell the other programs that you want "raw" keyboard events, which means that you won't really be receiving fully "cooked" characters. As a result, you will have to keep track of which keys are up and down, and how long, and handle all the odd meta key behavior in your program (that's not an 'a' it's a 'A' because shift is down, etc).

还需要考虑其他处理模式,例如规范和非规范处理,它们将控制您希望事件是以行定向的块(行事件)还是以字符定向的块(字符事件)接收.同样,由于需要使上游程序了解下游客户端的需求,因此使情况变得有些复杂.

There are also other processing modes to consider, like canonical and non-canonical, which will control whether you wish the events to be received in line oriented chunks (line events) or character oriented chunks (character events). Again this is somewhat complicated by the need to make the upstream programs aware of the requirements of the downstream client.

现在您已经对环境有所了解,让我们重新查看抑制字符输出所需的实际代码.

Now that you have some idea of your environment, let's revisit the actual code needed to suppress character output.

// define a terminal configuration data structure
struct termios term;

// copy the stdin terminal configuration into term
tcgetattr( fileno(stdin), &term );

// turn off Canonical processing in term
term.c_lflag &= ~ICANON;

// turn off screen echo in term
term.c_lflag &= ~ECHO;

// set the terminal configuration for stdin according to term, now
tcsetattr( fileno(stdin), TCSANOW, &term);


(fetch characters here, use printf to show whatever you like)

// turn on Canonical processing in term
term.c_lflag |= ICANON;

// turn on screen echo in term
term.c_lflag |= ECHO;

// set the terminal configuration for stdin according to term, now
tcsetattr( fileno(stdin), TCSANOW, &term);

即使这不是立即的.要立即执行操作,您需要更靠近源代码,这最终意味着一个内核模块(它仍不如键盘微控制器那么快速,而它不如开关实际关闭时那么即时).在源和目的地之间有足够的项目时,最终有可能注意到它们之间的差异,但是,在实践中,寻求性能与灵活性之间最佳权衡的人们已经在大量使用此代码.

Even this is not immediate. To get immediate, you need to get closer to the source, which eventually means a kernel module (which still isn't as immediate as the keyboard micro-controller, which isn't as immediate as the moment the switch actually closes). With enough items in between the source and the destination, eventually it becomes possible to notice the difference, however, in practice this code has been worked on a lot by people who are seeking the best tradeoff between performance and flexibility.

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

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