从标准输入捕捉人物,而无需等待输入要pressed [英] Capture characters from standard input without waiting for enter to be pressed

查看:108
本文介绍了从标准输入捕捉人物,而无需等待输入要pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我永远记得我是如何做到这一点,因为它出现如此罕见,对我来说。但在C或C ++,什么是从标准输入读取一个字符,而不必等待新行(preSS进入)的最佳方式。

I can never remember how I do this because it comes up so infrequently for me. But in C or C++, what is the best way to read a character from standard input without waiting for a newline (press enter).

也非常不会回显输入的字符在屏幕上。我只是想捕捉按键与出影响控制台屏幕。

Also ideally it wouldn't echo the input character to the screen. I just want to capture keystrokes with out effecting the console screen.

推荐答案

这是不可能的可移植性纯C ++,因为它取决于所使用的终端可能与标准输入连接(他们通常行缓冲)上太多。不过你可以使用图书馆为:

That's not possible portably in pure C++, because it depends too much on the terminal used that may be connected with stdin (they are usually line buffered). You can, however use a library for that:


  1. conio适用于Windows编译器。使用功能 _getch()给你一个角色,而无需等待回车键。我不是一个常见的​​Windows 8开发者,但我已经看到我的同学只是包括 CONIO.H 并使用它。请参见 CONIO.H 在维基百科。它列出了残培,这是德宣布在Visual C ++ pcated $ P $。

  2. 可用于Linux的诅咒,诅咒兼容实现可用于Windows了。它也有一个残培功能。 (尝试人残培来查看其联机帮助页)。请参见在Wikipedia诅咒

  1. conio available with windows compilers. Use the function _getch() to give you a character without waiting for the enter key. I'm not a frequent windows developer, but i've seen my classmates just include conio.h and use it. See conio.h at wikipedia. It lists getch, which is declared deprecated in Visual C++.
  2. curses available for linux, compatible curses implementations are available for windows too. It has also a getch function. (try man getch to view its manpage). See Curses at wikipedia.

我会建议你,如果你瞄准跨平台的兼容性使用诅咒。不过,我敢肯定有,你可以用它来关闭行缓冲(我相信这就是所谓的原始状态,而不是熟模式(看看男人stty的功能))。诅咒会处理,对于你在一个可移植的方式,如果我没有记错。

I would recommend you to use curses if you aim for cross platform compatibility. That said, I'm sure there are functions that you can use to switch off line buffering (i believe that's called "raw mode", as opposed to "cooked mode" (look into man stty)). Curses would handle that for you in a portable manner if i'm not mistaken.

这篇关于从标准输入捕捉人物,而无需等待输入要pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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