如何在C ++中读取单个击键 [英] how to read a single keystroke in C++

查看:53
本文介绍了如何在C ++中读取单个击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




可能是一个愚蠢的问题,但我找不到任何东西。


是否有一个istream相关功能,让我通过cin从键盘上读出一键击键




我需要它做的是:

- 删除当前输入缓冲区中的所有字符

- 阻止直到用户按下一个键

- 返回按下的键的ascii代码


如果用户只是按下回车键,这也应该有效。


我可能太傻了,不能正常使用,但通常会得到/ getline()

函数总是阻塞,直到用户点击进入,即使用户之前输入其他键,但另一方面,如果用户只按下回车键,

getline继续阻止并且不返回空行。


理想情况下我想这样想:


1.一个菜单,用户可以按1,2或3(一个d程序立即重新启动

,而无需用户随后按Enter键。

2. a按任意键继续功能,无论用什么键,都等待一个

击键。


Frank

解决方案

Frank Bormann写道:

可能是一个愚蠢的问题,但我找不到任何东西。

是否存在与之相关的问题让我从键盘通过cin读出一个键击的功能? [...]




没有''键盘''AFA C ++ _language_这样的东西。




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:cZ ******************* @ newsread1.mlpsca01.us.t o.verio.net ... < blockquote class =post_quotes> Frank Bormann写道:

可能是一个愚蠢的问题,但我找不到任何东西。

是否有与istream相关的功能让我通过cin从键盘上读出一个
击键? [...]



没有''键盘''AFA C ++ _language_这样的东西。




好的,让我提出不同的问题:我如何确定,如果有crs streambuf中有字符无限制地读取?b $ b?我曾尝试使用in_avail()和

readome(),但它们总是在cin上返回0,即使仍有输入

数据存在于缓冲区。


Frank Bormann写道:

" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
新闻:cZ ******************* @ newsread1.mlpsca01.us.t o.verio.net ...
< blockquote class =post_quotes> Frank Bormann写道:

可能是一个愚蠢的问题,但我找不到任何东西。

有没有一个istream相关的功能,让我从键盘通过cin读取一个
击键? [...]



没有''键盘''AFA C ++ _language_这样的东西。



好​​的,让我提出不同的问题:我怎样才能确定,如果在没有阻止的情况下在cin streambuf中有字符阅读?我曾尝试使用in_avail()和
readsome(),但它们总是在cin上返回0,即使缓冲区中仍然存在输入数据。




那么让我问你。如果''in_avail''或''readsome''返回0,那么是什么让你认为还有输入数据存在?我的意思是,如果你有一些其他方式知道流中有数据,为什么不用你用相同的方法读取它?
?抓住我的漂移?


Hi,

probably a stupid question, but I haven''t been able to find anything.

Is there a istream related function that let me read exactly one keystroke
from the keyboard through cin?

What I need it to do is this:
- remove all characters currently in the input buffer
- block until the user presses a key
- return the ascii code of the key that was pressed

This should work also, if the user just hit the enter key.

I''m probably too stupid to get it right, but the usual get/getline()
functions always block until the user hits enter, even if the user enteres
other keys before, but on the other hand, if the user presses enter only,
getline continues to block and does not return the empty line.

Ideally I would like somethink like this:

1. a menu, where the user can press 1, 2 or 3 (and the program reactiving
immediately without the need for the user to press enter afterwards)
2. a "Press any key to continue" function, that waits for exactly one
keystroke, no matter what key it is.

Frank

解决方案

Frank Bormann wrote:

probably a stupid question, but I haven''t been able to find anything.

Is there a istream related function that let me read exactly one keystroke
from the keyboard through cin? [...]



There is no such thing as ''keyboard'' AFA C++ _language_ is concerned.



"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:cZ*******************@newsread1.mlpsca01.us.t o.verio.net...

Frank Bormann wrote:

probably a stupid question, but I haven''t been able to find anything.

Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? [...]



There is no such thing as ''keyboard'' AFA C++ _language_ is concerned.



Ok, let me ask differently: How can I determine, if there are chars to read
in the cin streambuf without blocking? I''ve tried to use in_avail() and
readsome(), but they always return 0 on cin, even if there''s still input
data present in the buffer.


Frank Bormann wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:cZ*******************@newsread1.mlpsca01.us.t o.verio.net...

Frank Bormann wrote:

probably a stupid question, but I haven''t been able to find anything.

Is there a istream related function that let me read exactly one
keystroke
from the keyboard through cin? [...]



There is no such thing as ''keyboard'' AFA C++ _language_ is concerned.


Ok, let me ask differently: How can I determine, if there are chars to read
in the cin streambuf without blocking? I''ve tried to use in_avail() and
readsome(), but they always return 0 on cin, even if there''s still input
data present in the buffer.



Let me ask you, then. If ''in_avail'' or ''readsome'' returns 0, what makes
you think that "there''s still input data present"? I mean, if you have
some other way of knowing that there are data in the stream, why don''t
you use the same method to read it? Catch my drift?


这篇关于如何在C ++中读取单个击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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