我正在尝试编写一个C ++程序,其中当用户从键盘输入任何字符时,它应该移至下一行代码。
这是我的代码:
char c;
cin>>c;
cout<<"Something"<<endl;
但这是行不通的,因为只有当我输入一些字符然后按Enter时,它才会移动到下一行。
要么
如果我用这个
cin.get() or cin.get(c)
当我按Enter键时,它将移至下一条指令。
但是我希望它移动到键盘上按下的任何键的下一行,该怎么办?
我正在尝试编写一个C ++程序,其中当用户从键盘输入任何字符时,它应该移至下一行代码。
这是我的代码:
char c;
cin>>c;
cout<<"Something"<<endl;
但这是行不通的,因为只有当我输入一些字符然后按Enter时,它才会移动到下一行。
要么
如果我用这个
cin.get() or cin.get(c)
当我按Enter键时,它将移至下一条指令。
但是我希望它移动到键盘上按下的任何键的下一行,该怎么办?
Answers:
在Windows上:
system("pause");
在Mac和Linux上:
system("read");
将输出“按任意键继续...”,显然,等待任何键被按下。我希望那是你的意思
system调用外部程序。您无需调用外部程序即可等待任何键!这就像叫别人坐在您的计算机前打开计算机一样-这是很慢的解决方案。
如果您使用的是Windows,则可以使用kbhit()Microsoft运行时库中的一部分。如果您使用的是Linux,则可以kbhit这样实现(源):
#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;
}
更新:上面的功能在OS X上有效(至少在OS X 10.5.8-Leopard上有效,因此我希望它可以在OS X的最新版本上使用)。该要点可以另存为,kbhit.c并可以在Linux和OS X上使用
gcc -o kbhit kbhit.c
当与
./kbhit
它提示您进行按键操作,并在您击键时退出(不限于Enter键或可打印键)。
@Johnsyweb-请详细说明“详细的规范答案”和“所有问题”的含义。另外,请“跨平台”:通过此实现,kbhit()您可以在Linux / Unix / OS X / Windows上的C ++程序中具有相同的功能-您可能要引用其他平台?
@Johnsyweb的进一步更新: C ++应用程序不存在于密封的C ++环境中。C ++成功的一个重要原因是与C的互操作性。所有主流平台都是使用C接口实现的(即使内部实现使用的是C ++),因此您对“传统”的谈论似乎不合时宜。另外,当我们谈论单个函数时,为什么要为此使用C ++(“带有类的C”)?正如我所指出的,您可以用C ++编写并轻松访问此功能,并且应用程序的用户不太可能关心您如何实现它。
kbhit()函数terminal_settings在一行中声明了一个变量,该变量似乎什么也没做,但是却可以完成所有工作。有些人可能认为它很整洁,但是我发现它对于无法读取的点是晦涩的。
没有完全可移植的解决方案。
comp.lang.c常见问题的问题19.1在一定程度上涵盖了此问题,提供了适用于Windows,类Unix系统甚至MS-DOS和VMS的解决方案。
快速而不完整的摘要:
curses库;调用cbreak()后跟getch()(不要与Windows特定的getch()功能混淆)。请注意,curses通常控制终端,因此这可能是过大的。ioctl()用来操纵终端设置。tcgetattr()并且tcsetattr()可能是一个更好的解决方案。system()用来调用stty命令。getch()或getche()。SMG$)例程可以解决问题。所有这些C解决方案都应在C ++中同样有效;我不知道任何特定于C ++的解决方案。
在Windows中,此简短程序可以实现目标:getch暂停控制台,直到按下某个键为止(https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/)
#include<iostream.h>
#include<conio.h>
using namespace std;
void check()
{
char chk; int j;
cout<<"\n\nPress any key to continue...";
chk=getch();
j=chk;
for(int i=1;i<=256;i++)
if(i==j) break;
clrscr();
}
void main()
{
clrscr();
check();
cout<<"\n\nIt works!";
getch();
}
应该注意的是,getch不是标准库的一部分。
getch()与cin.get或有什么不同cin>>,也许有些文档链接
我调查了您要达到的目标,因为我记得我想做同样的事情。受Vinay启发我写了一些对我东西,我对此有些理解。但是我不是专家,所以请小心。
我不知道Vinay是如何知道您正在使用Mac OS X的,但是它在大多数类似unix的OS上都应该像这样工作。资源非常有帮助opengroup.org
使用该功能之前,请确保先冲洗缓冲区。
#include <stdio.h>
#include <termios.h> //termios, TCSANOW, ECHO, ICANON
#include <unistd.h> //STDIN_FILENO
void pressKey()
{
//the struct termios stores all kinds of flags which can manipulate the I/O Interface
//I have an old one to save the old settings and a new
static struct termios oldt, newt;
printf("Press key to continue....\n");
//tcgetattr gets the parameters of the current terminal
//STDIN_FILENO will tell tcgetattr that it should write the settings
// of stdin to oldt
tcgetattr( STDIN_FILENO, &oldt);
//now the settings will be copied
newt = oldt;
//two of the c_lflag will be turned off
//ECHO which is responsible for displaying the input of the user in the terminal
//ICANON is the essential one! Normally this takes care that one line at a time will be processed
//that means it will return if it sees a "\n" or an EOF or an EOL
newt.c_lflag &= ~(ICANON | ECHO );
//Those new settings will be set to STDIN
//TCSANOW tells tcsetattr to change attributes immediately.
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
//now the char wil be requested
getchar();
//the old settings will be written back to STDIN
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
int main(void)
{
pressKey();
printf("END\n");
return 0;
}
O_NONBLOCK似乎也很重要,但对我而言并没有任何改变。
如果有更深入的知识的人对此发表评论并提出建议,我将不胜感激。
这在Windows平台上有效: 它直接使用微处理器寄存器,可用于检查按键或鼠标按钮
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
union REGS in,out;
in.h.ah=0x00;
printf("Press any key : ");
int86(0x16,&in,&out);
printf("Ascii : %d\n",out.h.al);
char ch = out.h.al;
printf("Charcter Pressed : %c",&ch);
printf("Scan code : %d",out.h.ah);
getch();
}
您也可以使用conio.h中的getch()。像这样:
...includes, defines etc
void main()
{
//operator
getch(); //now this function is waiting for any key press. When you have pressed its just //finish and next line of code will be called
}
因此,由于UNIX没有conio.h,我们可以通过以下代码来模拟getch()(但是此代码已经由Vinary编写,我失败了):
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int mygetch( ) {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}