C++ cin 按键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11987717/
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++ cin keypress event
提问by Lukas Salich
I believe this is a very simple question, but I can't find a simple answer to it. I have an infinite loop, e.g. while(1)
, for(;;)
, and I need to break from the loop on a keypress. What is the easiest way to do this?
我相信这是一个非常简单的问题,但我找不到一个简单的答案。我有一个无限循环,例如while(1)
, for(;;)
,我需要在按键时中断循环。什么是最简单的方法来做到这一点?
P.S.: I can't use getch
, cin.ignore
, or cin.get
because it stops the loop.
PS:我不能使用getch
, cin.ignore
, 或者cin.get
因为它会停止循环。
回答by Bartek Banachewicz
Well, what you want is asynchronous input. All of the methods provided by cin
wait for enter. You will have to use system-specific functions for that, or use a library that will do it for you.
好吧,你想要的是异步输入。cin
wait for enter提供的所有方法。为此,您必须使用系统特定的函数,或者使用可以为您完成的库。
What you need to do is to not only process your logic in a while loop, but also listen from message pipe from your OS. If you want some more information about that one, please drop a comment.
您需要做的不仅是在 while 循环中处理您的逻辑,而且还要从您的操作系统的消息管道中进行监听。如果你想了解更多关于那个的信息,请发表评论。
EDIT: There's one other way, but I wouldn't recommend it as it can be non-portable I believe. The following code compiles and runs under VS2012RC.
编辑:还有另一种方式,但我不推荐它,因为我相信它可能是不可移植的。以下代码在VS2012RC下编译运行。
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout << "Enter a character";
getch();
}
回答by Software_Designer
Below is a Windows console code that uses kbhit()
and has an infinite loop.
But if keyboard is hit, it breaks the loop and exits.
If you have <conio.h>
, try this :
下面是使用kbhit()
并具有无限循环的 Windows 控制台代码。但是如果键盘被击中,它会中断循环并退出。如果你有<conio.h>
,试试这个:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
while (1)
{
if (kbhit()) break;
}
return 0;
}
回答by Kerrek SB
There is no "keyboard" in C++. You only have an opaque input data stream which your terminal populates occasionally with its own keyboard input. This is almost always a buffered, editable line-wise input, so you have no way of knowing when any given key was pressed.
C++ 中没有“键盘”。您只有一个不透明的输入数据流,您的终端偶尔会使用自己的键盘输入填充该数据流。这几乎总是一个缓冲的、可编辑的逐行输入,因此您无法知道何时按下了任何给定的键。
You need a platform-specific method to communicate directly with your terminal on a lower level. One such library, fairly wide-spread and portable, is ncurses
(a compatible Windows variant exists). Portable graphical application frameworks such as SDL and Allegro also provide raw keyboard handling.
您需要一种特定于平台的方法来直接与较低级别的终端进行通信。一个这样的库,相当广泛且可移植,是ncurses
(存在兼容的 Windows 变体)。SDL 和 Allegro 等便携式图形应用程序框架也提供原始键盘处理。
回答by huseyin tugrul buyukisik
This checks if "left arrow" is being pressed or not:
这将检查“左箭头”是否被按下:
GetKeyState(VK_LEFT)
Also this does not wait for anything. Just checks some flags.
此外,这不会等待任何事情。只是检查一些标志。
Some other keys defined in winuser.h:
winuser.h 中定义的其他一些键:
#define VK_NUMPAD0 0x60
#define VK_NUMPAD1 0x61
#define VK_NUMPAD2 0x62
#define VK_NUMPAD3 0x63
#define VK_NUMPAD4 0x64
#define VK_NUMPAD5 0x65
#define VK_NUMPAD6 0x66
#define VK_NUMPAD7 0x67
#define VK_NUMPAD8 0x68
#define VK_NUMPAD9 0x69
#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D
#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14
#define VK_KANA 0x15
#define VK_HANGEUL 0x15 /* old name - should be here for compatibility */
#define VK_HANGUL 0x15
#define VK_JUNJA 0x17
#define VK_FINAL 0x18
#define VK_HANJA 0x19
#define VK_KANJI 0x19
#define VK_ESCAPE 0x1B
#define VK_CONVERT 0x1C
#define VK_NONCONVERT 0x1D
#define VK_ACCEPT 0x1E
#define VK_MODECHANGE 0x1F
#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F
winuser.h must be included in windows.h
winuser.h 必须包含在 windows.h 中