Non blocking getch() - c++

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

Related

Function that gets char instantly without pressing enter

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.

Is there a way to make getch() wait until a key is pressed?

I'm currently working on a project using ncursor to handle animation and user input. The thing is that I want getchar() to only ask for user input when the keyboard is actually pressed. I did find a function which is called nodelay but it for some reason didn't actually delay getch().
My question is if there's a working alternative to this or if I might be using nodelay improperly. I've implemented nodelay into my code in this fashion:
nodelay(win, true);
while (inGame == true)
{
refresh();
printPlayGround();
wrefresh(win);
key = getchar();
setDirection(key);
moveSnake();
}
The thing is that it still asks for user input even if I don't press the keyboard. Could threads instead be the solution to my problem?
Ok so after looking into more what ncurser input really is i found wgetch(window variable). This is also probably why nodelay isn't working as i'm putting nodelay in one specific window but I'm using getch() on the "mainscreen". However while this does seemingly fix that issue, my code now only asks for user input once, while after that stopping to seemingly do it. But I've atleast come closer to getting it all working.

How can I detect if any key is pressed by the user in C++ (console)?

I am writing a C++ CLI application how can I detect if any key is pressed by the user. I've seen that in c# but how can it be implement in c++
while(1)
{
while(/* code to check if any key is pressed*/)
{ //rest of the code
// sleep function
}
}
Hint: like in CLI games to move or to take certain action when a key is pressed or don't do any thing if no input is given.
On windows at least you could use GetKeyState
we can use _kbhit() function in c++. _kbhit is equal to 1 if any key is pressed. You have to clear the _kbhit buffer else it will remain 1. Method for clearing is character = getch(); This will save the last entered key in character which you can compare and decide which action to perform on which key.
While loop can be CPU consuming, i do not advice busy waiting method, instead you should think of event hooking.
Here you can read about winapi keystroke event hooking C++ Win32 keyboard events
If you are still interested to use the while loop, you should also free some resources by sleeping after checking that a condition is false (e.g. nanosleep )

What is the fastest way to determine a key press and key holding in Win32?

What is the fastest way to determine a key press and also how to determine if a key is being held? It appears that window messaging is slow. Please provide an example of how to do so, and why it is faster than an alternative.
To be clear, this for a real time loop (a simulation) so I am looking for the fastest way to determine if a key has been pressed and also to check to see if it is being held.
GetAsyncKeyState() is what you're looking for. It reads the physical state of the keyboard, regardless of the input queue state. If the high-bit is set, then the key was down at the time of the call.
// Fetch tab key state.
SHORT tabKeyState = GetAsyncKeyState( VK_TAB );
// Test high bit - if set, key was down when GetAsyncKeyState was called.
if( ( 1 << 15 ) & tabKeyState )
{
// TAB key down...
}
Also, for the record, Windows is not a real-time operating system. If your application requires real-time precision, you may want to select another platform.
If you just want to poll the keyboard state so as to discover which keys are up/down as well as the shift/alt/ctrl state, just call GetKeyboardState (MSDN reference).
When I worked in a game studio, this is exactly how we got keyboard state for each frame. Should be applicable to your simulation code.
TL;DR: you can use GetAsyncKeyState for checking if a key is currently down, but for best application responsiveness to key presses and releases, you want to use the Win32 pipeline code near the bottom of my post.
GetAsyncKeyState works perfectly fine for determining if a key is currently down, but in terms of determining whether a key was first pressed or released and how many times this was done, GetAsyncKeyState misses keystrokes in a CPU-intensive application, even after storing the previous key state.
This was what I tried:
static const unsigned int NumberOfKeys = 256U;
bool previousKeyboardState[NumberOfKeys];
//Get the current state of each key as the application starts to ensure that keys held down beforehand are not processed as pressed keys.
for (unsigned int keyNum = 0U; keyNum < NumberOfKeys; ++keyNum)
{
previousKeyboardState[keyNum] = isKeyDown(keyNum);
}
//Works fine.
bool isKeyDown(int key)
{
return (GetAsyncKeyState(key) & (1 << 16));
}
//Misses key presses when application is bogged down.
bool isKeyFirstPressed(int key)
{
bool previousState = previousKeyboardState[key];
previousKeyboardState[key] = isKeyDown(key);
return (previousKeyboardState[key] && !previousState);
}
//Misses key releases when application is bogged down.
bool isKeyFirstReleased(int key)
{
bool previousState = previousKeyboardState[key];
previousKeyboardState[key] = isKeyDown(key);
return (!previousKeyboardState[key] && previousState);
}
//Example usage:
if (isKeyDown(VK_W))
{
//W key.
}
if (isKeyFirstReleased(VK_SNAPSHOT))
{
//Print screen.
}
GetKeyboardState is no good either, as it does not keep track of the number of key presses or releases. As Erik Philips said in his answer, these are unbuffered solutions, which are no good if you are e.g. writing a game. You would have to process all keystrokes faster than they are received.
Now, my code above works decently well, and may be suitable for many people, but I much prefer not to miss a single keystroke. I hate using applications that are unresponsive. I think the best solution for Win32 applications is to catch WM_KEYDOWN and WM_KEYUP messages in the pipeline and process them. What's nice is that WM_KEYDOWN also provides an auto-repeat count, which could be useful for applications that support entering text (e.g. chat, IDE's, etc.). This also adds a slight complication, which is mentioned in the WM_KEYDOWN documentation:
Because of the autorepeat feature, more than one WM_KEYDOWN message
may be posted before a WM_KEYUP message is posted. The previous key
state (bit 30) can be used to determine whether the WM_KEYDOWN message
indicates the first down transition or a repeated down transition.
There are also Windows keyboard hooks you could look into, but those are more difficult to use. They're good for receiving global key presses though.
Considering that all inter-windows communications are through windows messaging (keyboard events, mouse events, pretty much all events you can imagine), there isn't a lower level way to access the keyboard events (unless you write your own keyboard driver) that I know of.
DirectX still uses the windows keyboard messaging to provide DirectX programmers easier access to keyboard events.
Updated
My note about DirectX was not to use it, but that when Microsoft wanted to make an interface for programmers to use for real time games, they still wrote DirectX on top of the Windows Message Queue.
I would suggest taking a look at how to write a program that can read directly from the message queue. I believe there is a good example Code Project Windows Message Handling - Part 1.
Your two options are to either read from the message queue (buffered) or read directly from the keyboard state (as Bukes states) which means your own loop could techinically miss a keyboard event for any number of reasons.

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.