C++ 按键:getch,cin.get?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7010760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ keypress: getch, cin.get?
提问by CaptainProg
I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn't matter whether I use 'any key' or a specific key, but I need to have the program freeze until I press something.
我有一个循环运行的 Win32 程序。我希望能够在等待按键时暂停该程序。我使用“任意键”还是特定键都没有关系,但是我需要让程序冻结,直到我按下某个键。
I am wondering which command I should use. I am working with Visual C++ and the compiler doesn't recognise any of the following commands:
我想知道我应该使用哪个命令。我正在使用 Visual C++ 并且编译器无法识别以下任何命令:
cin.get()
std::cin.get()
getch()
I am relatively new to C++. I understand that in a console app this is a fairly simple action to take (cin.get), but that it can be more difficult in Win32. Any simple solution or workaround would be appreciated. The program is bespoke to be used in a single scientific experiment, so for now I'm not fussed if the solution is a little botchy(!)
我对 C++ 比较陌生。我知道在控制台应用程序中这是一个相当简单的操作 (cin.get),但在 Win32 中可能会更困难。任何简单的解决方案或解决方法将不胜感激。该程序是专门用于单个科学实验的,所以现在我不会因为解决方案有点拙劣而大惊小怪(!)
Apologies if I've missed any important info from my question.
如果我错过了我的问题中的任何重要信息,我深表歉意。
回答by Armen Tsirunyan
You should use neither.
你不应该使用。
You should use
你应该使用
#include <iostream>
...
int main()
{
...
std::cin.ignore(); //why read something if you need to ignore it? :)
}'
回答by Daniel Blom
Example:
例子:
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Press any key to continue . . ." << std::endl;
_getch(); // wait for keypress
}
_getch()
is C++ equivalent to C getch()
_getch()
C++ 等价于 C getch()
回答by Jason
Try
尝试
#include <iostream>
using namespace std;
char temp;
cin >> temp;
回答by xeon111
Assuming that you are looking for an alternative for getch ( which does not echo to screen).
假设您正在寻找 getch 的替代方案(它不会回显到屏幕)。
If you are using windows and visual studio to be precise try using _getch. Here is a link to it http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx
如果您使用的是 Windows 和 Visual Studio,请尝试使用 _getch。这是它的链接 http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx
回答by Tony The Lion
You should #include <iostream>
and use std::cin.get();
你应该#include <iostream>
并使用std::cin.get();
I think the getch()
is a C function, but since you are using C++, then the cin
would be more appropriate.
我认为这getch()
是一个 C 函数,但既然你使用的是 C++,那么 thecin
会更合适。
回答by Dmitriy Yurchenko
HWND hwnd = ::GetConsoleWindow();
while (!((::GetForegroundWindow() == hwnd) &&
((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
::Sleep(0);
Suppose it is not the best way but it solved my problem. Replace VK_SPACE with any other value you like. And it is not portable.
假设这不是最好的方法,但它解决了我的问题。将 VK_SPACE 替换为您喜欢的任何其他值。而且它不便携。