-2

I'm new to programming, and I try to program a menu in C++.

I want it to be in a while loop, breaking when ESC is pressed, but I want the character to be read instantly without having to press Enter.

while (breaker != 27)
{
    //menu based on switch(breaker)
}

I found the getch() function and <conio.h> header, but people say not to use it because it works only on Windows.

Is there any other method that I can get a character without pressing Enter, and it's multiplatform/meets coding ethic?

My operating system is Windows 11, but I would like to know the solution that works on other systems.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Radek
  • 11
  • 1
  • 1
    That's not something you can do with standard C++, it's controlled by the terminal. But depending on the operating system and the terminal, it can be done in other ways. – Some programmer dude Jan 29 '23 at 18:55
  • This is a limitation (feature) of the console. I/O is under the purview of the operating system, so any I/O scheme you want will need to be supported by the OS you're using. What OS do you want to support? – JohnFilleau Jan 29 '23 at 18:56
  • Thanks, so If I would use other type of "medium" such as some graphic engine (I found builder in tutorials) I could do it other way? – Radek Jan 29 '23 at 19:01
  • Yes, you need some sort of graphics library that has already made the heavy lifting for it to be cross-platform. SFML and Qt comes to mind depending on your use-case. – Captain Giraffe Jan 29 '23 at 19:06
  • NCurses library can be used for this : https://invisible-island.net/ncurses/man/curs_getch.3x.html. Home page : https://invisible-island.net/ncurses/announce.html#h3-documentation – Pepijn Kramer Jan 29 '23 at 19:15
  • Voted to reopen. The question is precise and clear. Closing it for needing “details or clarity” is inappropriate. – Pete Becker Jan 29 '23 at 19:28
  • 1
    There are libraries and system-specific function which can bypass the line-buffering on the console or terminal, but it's not possible with plain standard C++. So please [edit] your question to tell us your operating system. – Some programmer dude Jan 29 '23 at 19:55
  • I edited my question, so you can see my OS type – Radek Jan 29 '23 at 21:05

1 Answers1

1

It is generally not possible to use the functions provided by the ISO C++ standard library to detect whether the ESC key has been pressed. However, most platforms provide a platform-specific API which does provide this functionality.

On Microsoft Windows, you can use the functions _getch, _getche or ReadConsoleInput.

On Linux, you can use the ncurses library.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Thanks, I'll use _getch then, It's simpliest (for me) I think. – Radek Jan 29 '23 at 21:03
  • @Radek: `_getch` and `_getche` are simplest to use, but `ReadConsoleInput` is more flexible. For example, when using `ReadConsoleInput`, you can wait for user input with a timeout, by combining that function with `WaitForSingleObject`. See [this answer of mine to another question](https://stackoverflow.com/a/72394274/12149471) for an example. – Andreas Wenzel Jan 29 '23 at 22:26