getch returns 2 characters when I type one(当我输入一个字符时,getch 返回 2 个字符)
问题描述
当我使用 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>
", 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"
从微软得到以下回复:
" 感谢您报告此问题!这将在未来的窗口中修复更新."
" 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 个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我输入一个字符时,getch 返回 2 个字符
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
