Whenever I type some characters into the windows console and hit enter, it automatically scrolls to the next line. Is there any way to disable this behavior in C++ (using the Windows API), and if so; how?
Asked
Active
Viewed 290 times
1 Answers
1
If you call scanf
or getline
or similar then the underlying C runtime (CRT) handles Enter, Backspace, Delete, arrow keys, Tab, and such, and of course all printable keys.
If you want to handle Enter differently from CRT then you will have to handle every other key as well, using _getch
(nonstandard function different from getchar
). You will have to write some code. As far as I know there is no way to use scanf
or getline
, without Enter going to the next line.

Dialecticus
- 16,400
- 7
- 43
- 103
-
As far as I know, the functions you mentioned above read console input in much the same way I do (through Win32 API). This doesn't solve the issue, because the problem seems to be with the low-level console input rather than the high-level console character streams. – invertedPanda Jan 22 '20 at 12:23
-
@invertedPanda please read the article for [_getch](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019) on Microsoft Docs. This function will read the character without buffering and without echoing. `getchar` on the other hand reads from the buffer (that requires for user to press `Enter`) and the keypress is echoed. You want to disable that echo, so that `Enter` does not in fact moves the cursor to a new line. That is why you need `_getch`. – Dialecticus Jan 22 '20 at 14:09
-
Ah, you are in fact correct. Would you also know of a more low-level version of _getch I can use (i.e. one I can supply a console HANDLE to)? – invertedPanda Jan 22 '20 at 21:26
-
Check out [console functions](https://learn.microsoft.com/en-us/windows/console/console-functions), `ReadConsole` in particular. – Dialecticus Jan 22 '20 at 22:32