如何模拟“按任意键继续?” [英] How to simulate "Press any key to continue?"

查看:192
本文介绍了如何模拟“按任意键继续?”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C ++程序,当用户从键盘输入任何字符,它应该移动到下一行代码。



这里是我的代码:

  char c; 

cin>> c;

cout<<Something<< endl;

但这不工作,因为它只会移动到下一行当我输入一些字符,然后按ENTER。





如果我使用

  cin.get()或cin.get(c)

它按下Enter键时移到下一行指令。



但我想让它在键盘上按下的任何键上移动到下一行,完成?



感谢。

解决方案

,您可以使用 kbhit()这是Microsoft运行时库的一部分。如果你在Linux上,你可以实现 kbhit source ):

  #include< stdio.h> 
#include< termios.h>
#include< unistd.h>
#include< fcntl.h>

int kbhit(void)
{
struct termios,newt;
int ch;
int oldf;

tcgetattr(STDIN_FILENO,& oldt);
newt = oldt;
newt.c_lflag& =〜(ICANON | ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,& newt);
oldf = fcntl(STDIN_FILENO,F_GETFL,0);
fcntl(STDIN_FILENO,F_SETFL,oldf | O_NONBLOCK);

ch = getchar();

tcsetattr(STDIN_FILENO,TCSANOW,& oldt);
fcntl(STDIN_FILENO,F_SETFL,oldf);

if(ch!= EOF)
{
ungetc(ch,stdin);
return 1;
}

return 0;
}

更新: (至少在OS X 10.5.8 - Leopard,所以我希望它可以在更新的OS X版本上工作)。此 gist 可另存为 kbhit.c ,并在Linux和OS X与

  gcc -o kbhit kbhit.c 

使用

运行时

  ./ kbhit 

它会提示您输入一个按键,当您按下一个键(不限于输入或可打印键)时才会退出。



@Johnsyweb - 请详细说明详细规范答案和所有关注的含义。还有,跨平台:使用 kbhit()的实现,可以在Linux / Unix / OS X / Windows上的C ++程序中具有相同的功能 -

更新@Johnsyweb :C ++应用程序不存在于密封的C ++环境中。 C ++的成功的一个重要原因是与C的互操作性。所有主流平台都是用C接口实现的(即使内部实现使用C ++),所以你对遗留的讨论似乎不合适。另外,当我们谈论单个函数时,为什么需要C ++(C with classes)?正如我所指出的,您可以使用C ++编写并轻松访问此功能,您的应用程序的用户不太可能关心您是如何实现的。


I am trying to write a C++ program in which when user enter any character from keyboard and it should move to next line of code.

Here is my code:

char c;

cin>>c;

cout<<"Something"<<endl;

but this is not working, because it only move to next line when I input some character and then press ENTER.

OR

If I use this

cin.get() or cin.get(c)

it move to next line of instruction when I press Enter.

But I wanted it to move to next line on any key pressed on the keyboard, how this can be done?

Thanks.

解决方案

If you're on Windows, you can use kbhit() which is part of the Microsoft run-time library. If you're on Linux, you can implement kbhit thus (source):

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

  ch = getchar();

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }

  return 0;
}

Update: The above function works on OS X (at least, on OS X 10.5.8 - Leopard, so I would expect it to work on more recent versions of OS X). This gist can be saved as kbhit.c and compiled on both Linux and OS X with

gcc -o kbhit kbhit.c

When run with

./kbhit

It prompts you for a keypress, and exits when you hit a key (not limited to Enter or printable keys).

@Johnsyweb - please elaborate what you mean by "detailed canonical answer" and "all the concerns". Also, re "cross-platform": With this implementation of kbhit() you can have the same functionality in a C++ program on Linux/Unix/OS X/Windows - which other platforms might you be referring to?

Further update for @Johnsyweb: C++ applications do not live in a hermetically sealed C++ environment. A big reason for C++'s success is interoperability with C. All mainstream platforms are implemented with C interfaces (even if internal implementation is using C++) so your talk of "legacy" seems out of place. Plus, as we are talking about a single function, why do you need C++ for this ("C with classes")? As I pointed out, you can write in C++ and access this functionality easily, and your application's users are unlikely to care how you implemented it.

这篇关于如何模拟“按任意键继续?”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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