为什么残培()之前preSS返回任意键? [英] Why getch() returns before press any key?

查看:218
本文介绍了为什么残培()之前preSS返回任意键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(int argc, char *argv[], char *env[])
{
    printf("Press any key to exit.\n");
    getch();
    return 0;
}

据该男子页,

残培应该等到任何键pssed $ P $

getch should wait until any key is pressed

...但实际上它直接preSS任意键返回之前。 (返回的值是 1 )。

...but in fact it returns directly before press any key. (The value returned is -1).

为什么?

更新

我在Linux上。我如何能实现 preSS任意键退出。,如果不使用残培()

I'm on Linux. How can I implement Press any key to exit., if not using getch()?

的getchar()只会返回后,preSS输入,这不是我想要的。

getchar() will only return after press Enter, it's not what I want.

推荐答案

在Linux中,残培()可能是诅咒功能,这是从同一个名字的特定Windows的功能完全不同。 诅咒的ncurses )可能是你想要做什么矫枉过正。

On Linux, getch() is probably the curses function, which is quite different from the Windows-specific function of the same name. curses (ncurses) is probably overkill for what you want to do.

最简单的办法是等待,直到用户presses <大骨节病>输入,你可以这样做:

The simplest approach would be to wait until the user presses Enter, which you can do like this:

int c;
printf("Press <enter> to quit: ");
fflush(stdout);
while ((c = getchar()) != '\n' && c != EOF) {
    /* nothing */
}

如果你真的希望用户能够preSS的任何的关键,而不仅仅是<大骨节病>输入,你可以做这样的事情:

If you really want the user to be able to press any key, not just Enter, you can do something like this:

system("stty cbreak -echo");
getchar();
system("stty cooked echo");

第二个的stty 旨在恢复TTY合理设置(它应该将其恢复到任何他们,但保存和恢复状态是一个有点复杂)。有可能是更清洁的方式做到这一点(使用任何库函数的stty 程序本身使用)。

The second stty is intended to restore the tty to reasonable settings (it should really restore them to whatever they were, but saving and restoring the state is a little more complicated). There are probably cleaner ways to do that (using whatever library functions the stty program itself uses).

编辑:读取单个字符,而无需等待<大骨节病>输入是经常被问到的问题。事实上,这是问题19.1在 comp.lang.c常见问题解答

Reading a single character without waiting for Enter is a frequently asked question. In fact, it's question 19.1 in the comp.lang.c FAQ.

EDIT2:我还没有对诅咒很多工作最近,但我只是做了一些与它玩耍。看来,残培()不会工作,除非你第一个电话 initscr的() - 和 initscr的()清除屏幕。诅咒是用于像那些需要显示的完全控制文本编辑器应用。有可能是使用残培()不考虑屏幕的控制的一种方式,但我还没有发现它。

I haven't done much work with curses recently, but I just did some playing around with it. It appears that getch() won't work unless you first call initscr() -- and initscr() clears the screen. curses is intended for use with applications like text editors that need full control of the display. There may be a way to use getch() without taking control of the screen, but I haven't found it.

系统(stty的...)杂牌实际上可能是最好的办法。

The system("stty ...") kludge might actually be the best approach.

EDIT3:的termios 在对方的回答解决方案可能是最好的(系统(stty的... )更简单,但是调用外部程序感觉就像矫枉过正。

The termios solution in the other answer is probably the best (system("stty ...") is simpler, but invoking an external program feels like overkill.

对于OP的评论我简直不敢相信 preSS任意键退出。是这么麻烦在C做,是的,这似乎奇 - 但进一步的思考也有它合理的原因。

As for the OP's comment "I can't believe Press any key to exit. is so troublesome to do in c", yes, that does seem odd -- but on further thought there are valid reasons for it.

看看安装了一个典型的Unix或Linux系统上的程序。我想你会发现,他们很少需要这种投入(等待一个键preSS)。

Take a look at the programs installed on a typical Unix or Linux system. I think you'll find that very few of them require this kind of input (waiting for a single keypress).

很多程序与文件或标准输入读取命令行参数和数据。任何用户类型的输入数据,而不是命令或响应提示。

A lot of programs work with command line arguments and data read from files or from stdin. Anything the user types is input data, not commands or responses to prompts.

有些程​​序要求确认的一些行动(安装像 apt-get的 CPAN 经常这样做) - 但他们通常读行的输入的的和检查的第一个字符。或者,对于一些激烈行动,他们可能会要求你输入整个单词是,然后按<大骨节病>输入(你不想重新格式化您的硬盘驱动器,因为你不小心撞到一个键)。

Some programs do ask for confirmation for some actions (installers like apt-get and cpan often do this) -- but they usually read a line of input and check the first character. Or, for some drastic actions, they might require you to type the whole word "yes" followed by Enter (you don't want to reformat your hard drive because you accidentally hit a key).

当然很多程序(文本编辑器,文件浏览器)读取单字符非呼应输入,但是这种方案往往是诅咒为主;他们采取了整个终端窗口的控制权。

Of course a lot of programs (text editors, file viewers) read single-character non-echoing input, but such programs tend to be curses-based; they take control of the entire terminal window.

最后,一些方案具有GUI界面(web浏览器等);他们可能甚至不从标准输入读取。

Finally, a number of programs have GUI interfaces (web browsers, etc.); they probably don't even read from stdin.

大多数生产的Unix程序不使用或需要 preSS任意键退出提示。他们只是做他们的工作,经常保持沉默,然后终止,所以你可以做的下一件事。需要在很大程度上存在着相对的基本程序,如家庭作业。不是说有什么不对的家庭作业,但整个系统并非主要设计用来支持这种用法。

Most production Unix programs don't use or need Press any key to exit prompts. They just do their jobs, often silently, and then terminate so you can do the next thing. The need exists largely for relatively elementary programs, such as homework assignments. Not that there's anything wrong with homework assignments, but the system as a whole isn't primarily designed to support such usage.

这篇关于为什么残培()之前preSS返回任意键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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