我输入一个时getch返回2个字符 [英] getch returns 2 characters when I type one
问题描述
当我使用getch时,它总是将读取的角色附加一个空字符。
When I use getch, it always appends the caracter read with a null character.
当我使用以下代码时:
#include "stdafx.h"
#include <conio.h>
int main()
{
char c = 0;
while (c != 'x')
{
c = _getch();
printf("Char read: <%c> \n", c);
}
}
当我按时,它将返回控制台上的跟随键 asdx:
It returns the follwing on the console, when I press the keys "asdx":
Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>
这是在VS 2017中编译的一个普通的新单个文件项目,在Windows 10控制台窗口上运行。我尝试删除_UNICODE和UNICODE定义。
This is compiled in VS 2017 in a plain new single file project, running on a windows 10 console window. I tried removing the _UNICODE and UNICODE define.
推荐答案
好吧,废话!
这是一个(相当新的) Windows中的错误。
It's a (rather new) bug in windows.
> https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html
使用_getch()函数构建控制台应用程序时,每次按键时
突然返回两次
"When building a console application using the _getch() function suddenly returns two times for each keypress"
获得了微软的以下答复:
got the following response from microsoft:
感谢您举报此问题!此问题将得到解决。在将来的Windows
更新中。
" Thanks for reporting this! This will be fixed on a future windows update."
更新:
如上面的链接所述,设置运行时静态链接与 C运行时的先前版本将解决此问题,但是您需要确保所有相关项目(如果有问题)例如,一个库)也使用相同的运行时。 (我测试过)
UPDATE: As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)
这篇关于我输入一个时getch返回2个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!