Function that gets char instantly without pressing enter - c++

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.

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.

Related

How to prevent enter key on windows console from automatically scrolling

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?
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.

Most suitable way to read keyboard input in C++

I'm trying to write a Keyboard class that can read in the keyboard buttons. I have looked at this link - http://www.daniweb.com/software-development/cpp/code/216732/reading-scan-codes-from-the-keyboard But as stated on there, it is not very accurate for all computers (I don't know if this is even true). Therefore, my question is whats the best method in implementing my keyboard class? This will be used for Windows
Many thanks
There are three ways to read keyboard input:
By reading input from a console window as described in your link. It's true that it's hard to get this to work correctly, for starters because it's reading ANSI characters and not Unicode characters, but there are other issues. Console input/output is kind of obscure, as is the documentation for it
By handling UI events associated with a normal window. In this case you would handle the WM_KEYDOWN message in a window procedure
By going deep into the Win32 API with functions like SetWindowsHookEx. In this case you don't even need a window (normal or console), and you can read keystrokes pressed in any application or in the desktop
It's hard to suggest which one to use without knowing how you intend to use this Keyboard class.

If loop that executes when Enter key is pressed C++

I'm using Windows XP Home Edition. I want to set up a menu system that displays an image when the Enter key is pressed. Is there any way I can set up an if statement that executes when the Enter key is pressed on the keyboard without setting up a GUI. For example, can I just use the hex value associated with Enter key as a trigger for the if statement.
I've looked into reading input buffers. Am I going to have to get that complex with it? I don't really
For windows there are a couple of variants you could use to implement this:
The standard way would be to call e.g. std::getline and discard the input.
A more Windows-specific solution is to use e.g. _kbhit and _getch to check for the Enter key.

Non blocking getch()

I'm trying to make Tetris game in standard console. I need non-blocking getch(), so the blocks can fall without pressing any key. It would be nice to have function that returns -1 if no key pressed, otherwise the key code.
This is exactly what you wanted:
int getch_noblock() {
if (_kbhit())
return _getch();
else
return -1;
}
Basically kbhit() does the job of determining if a key is pressed.
Assumes Windows and Microsoft Visual C++.
It's operating system specific but your library probably has a function called kbhit() or similar that will do this

Using keypress to switch a bool

I am writing a Windows C++ app which I would like to have detect a keypress (for this example, using the letter 'S'). When the key is pressed, the program should switch a bool value either on or off (depending on its current state).
I know that in console apps you can use cin.get, but I'm unfamiliar with the Win32 API. I also would like to be sure that when the key is pressed, the event is only registered once, i.e. if the user presses 'S' but holds the key down for a while, the program should detect only 'S'; not 'SSSSSSS'.
So you have a windows message loop going, right? Capture WM_KEYDOWN and check whether it's on autorepeat.
But I'd guess you are actually using a framework, be it MFC, QT or something else. The framework will wrap your windows message loop and allow you to capture key events, but if you want us to tell you how, you'll have to say what framework you're using.