简体   繁体   English

仅当我在c ++(Thread)中按Enter键时,代码如何进入下一步

[英]How the code gets to the next step only when i press the enter key in c++ (Thread)

I saw the following code on the internet as 我在互联网上看到以下代码

DWORD qThreadID;
HANDLE hThread = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);

// Loop until the user enters 'q'
char cChar = ' ';
while (cChar != 'q') {
    cout << uiCounter << endl;
    cChar = (char)getchar();
}

how does the keypress event "Enter" works on it? 按键事件“ Enter”如何工作? (when i debug it except for the press of "Enter" no other keypress functionality works ) Thanks (当我调试它时,除了按下“ Enter”键外,其他按键功能均无效)

getchar() reads from standard in, which is buffered, both in the library and in the OS. getchar()从库和操作系统中的标准缓冲中读取。 The usual OS's won't return from a read on a console device until enter is entered; 在输入enter之前,通常不会从控制台设备上的读取返回操作系统。 they support command line editing, and require the enter key to finalize the input. 它们支持命令行编辑,并且需要Enter键来完成输入。

getchar() reads a single character of input. getchar()读取输入的单个字符。

However, your terminal likely does line buffering on the input, no input is sent to your program until you hit enter. 但是,您的终端可能会在输入上进行行缓冲,直到您按下Enter键,才将输入发送到程序。

因为enter输入一个\\n ,它将被解释为EOF因为命令行会认为这是用户输入的结尾,并且getChar()试图从该流中读取单个字符缓冲区,因此如果您只是按enter而不返回任何内容,在其前面输入字符,请参阅msdn: http : //msdn.microsoft.com/zh-cn/library/5231d02a%28v=vs.71%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中执行Enter键上的代码? - how to execute code on enter key press in c++? 在获取下一个字符后如何正确刷新输入缓冲区,而不必两次按Enter键! C ++ - How do I flush the input buffer correctly after I get the next char without having to press enter twice! C++ 如何在c ++中实现“按下进入” - How to implement "press to enter"in c++ 为 C++ 在 wxWidgets 中手动触发 wxTextCtrl 上的 ENTER 按键事件? - Manually trigger an ENTER key press event on wxTextCtrl in wxWidgets for C++? 为什么C ++中的“按Enter继续”代码不起作用? - Why is this “Press Enter to Continue” code in C++ not working? 当从键盘输入数据时,当用户在C ++中按“输入”时如何完成输入? - when inputing data from keyboard, how to complete input when user press “enter” in C++? 自动完成功能不适用于 VS c++ 主要是当我按下回车键时它只是插入一个新行 - Autocomplete isn't working for VS c++ mainly when i press enter it just inserts a new line 线程运行时C ++按下箭头键 - C++ get arrow key press while a thread is running 按Enter按钮时如何使用C ++ Win32 API调用按钮? - How to invoke a button using C++ Win32 API when press the enter button? 如何编写一个子类QTextEdit以便在“ enter”按键上引起插槽调用? - How to code a subclassed QTextEdit to cause a slot call on a “enter” key press?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM