C ++在Windows中改变规范模式 [英] C++ change canonical mode in windows
问题描述
找到一堆线程后,我没有找到任何在Windows上,只有Linux,Ubuntu等。
After finding a bunch of threads on this I did not find any on Windows, only Linux, Ubuntu etc.
总之,我想知道的是如何关闭规范模式,以便在控制台中的输入导致即时输入,而不必按Enter键。
In short what I am trying to find out is how to turn canonical mode off so that input in the console results in instant input without having to press enter.
较长版本。这是我现在想做的:
当PGM暂停(基于文本的游戏)时,用户被要求输入箭头键来移动2D阵列上的播放器。但是 cin.get()
, cin.ignore()
Longer version. This is what I am trying to do right now:
When the PGM is pausing (text based game) the user is asked to enter an arrow key to move the player around on the 2D array. But cin.get()
, cin.ignore()
and some other things I've tried all require pressing enter before it will continue.
臭名昭着的系统(pause> nul);
em>实际工作,但因为我在各种其他地方阅读是非常糟糕的做法。
The infamous system("pause>nul");
does actually work, but as I've read in various other places is very bad practice.
这是代码的一部分。 cout
语句仅用于测试目的:
This is part of the code. the cout
statements are only for testing purpose:
//While loop
if(GetAsyncKeyState(VK_UP)){
cout << "up" << endl;
}
else if(GetAsyncKeyState(VK_DOWN)){
cout << "down" << endl;
}
else if(GetAsyncKeyState(VK_LEFT)){
cout << "left" << endl;
}
else if(GetAsyncKeyState(VK_RIGHT)){
cout << "right" << endl;
}
else{
break;}
//pauze and check for arrow key input here
推荐答案
也可以在Windows上使用 getch
:
You can use getch
also on windows:
我知道它是C,你得到了废弃的警告,但它工作...
I know it is C, and you got the deprecated warning, but it works...
循环,直到您按Enter:
this code run in a loop till you press Enter:
检查当您按箭头键时发生什么...
check what happen when you press an arrow key...
#include <stdio.h>
#include<conio.h>
int main ()
{
int c;
do {
c=getch();
printf("%d\n",c);
} while (c != 13);
return 0;
}
这篇关于C ++在Windows中改变规范模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!