I'm making a simple program using the graphic mode in C++, I'm having trouble dealing with the input.
I just need a way to check the keyboard and, if any key has been pressed, store the key value on a variable,
the problem with getch() is that it waits untill any key has been pressed, and I need the programm to keep going if the user is not giving input.
I've made a research and it seems that there are hundreds of ways of doing this.
which one is the easiest?
Example: the loop begins, the program checks if there has been any input and stores it, the program works with that input and then discards it to begin the loop again. That would work perfectly with getch() isn't it? Well I want the loop to also keep going if no key has been pressed this time. A sort of no input default mode so the process doesn't stop.
(what I would really like is an equivalent to getch() that does'nt wait for user input).
Windows, Code::Blocks IDE, GCC compiler
yeah, kbhit seems to work as expected, Thanks!!
Use int _kbhit( void ); : it's a non-blocking call to getch()(works only on Windows).
It's also included in conio.h
Doc here : http://msdn.microsoft.com/en-US/en-en/library/58w7c94c.aspx
(There is also a workaround for *nix systems : http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html )
Related
I read this link Pause Console in C++ program and I don't still get the correct answer based on my needs.
I want a function that behaves exactly the same with system("pause") but it should be portable and has no additional prints.
I also don't want std::cin.get() and getchar() because it still echoes the characters except for the enter key.
getch() still works but it is not portable.
Goal:
Will proceed after pressing any key, not only the enter key. (remember: only 1 key)
Will not use any libraries aside from the standard library.
So, if you don't really know what do I mean, there's a small explanation. I'm also new in C++ so I need help with some things, so yeah. There's the explanation.
If you make a code, for example
cout << "Hello world!";```
-- I forgot how to make it fully good, but that's an example
Then it prints and closes extremelly fast. So, how can I make it so that it DOESN'T dissapear?
I am assuming you are talking about console window that closes fast.
In order to prevent that there are multiple ways of doing it. But i will write two simplest ways of doing so.
Add header at the top of your C++ program
#include <stdlib.h>
Then in the end of main function just before return 0 write this line of code
system("pause");
Another way of doing this is to get any character input before return 0. So write these lines of code
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
One of the simplest solutions is to use std::getchar() or
std::cin.get(). The program will wait for user input.
However, if there are already characters in the input buffer, then the program, roughly speaking, will ignore and will not wait for input from the user.
If the program is specific to Windows, then one of the most common solutions is to use std::system("pause").
For std::system(), you must also include the cstdlib header.
But it would still be better to set a breakpoint at the end of the program or, if your IDE allows (for example Visual Studio), then enable the option that leaves the console opened even after exiting. For example, VS allows you to run a program in non-debug mode, which does not close the console. To run the program in this mode, you can press the key combination Ctrl + F5.
However, in this case, in the project settings there should be Subsystem option in the System section in the Linker. And the value of Subsystem should be /SUBSYSTEM:CONSOLE.
I have a main routine that loops infinitely. By changing bool variables using keyboard input, I want to be able to control whether certain if{} statements within that loop are getting called. I found this thread:
C non-blocking keyboard input,
but it seems excessively laborious and complicated for seemingly basic functionality. Is there an easier way to do it?
You'll have to use the OS/Firmware/Framework/environment API to get input events, or use a library that do this for you. Anyway, there is no built-in way of doing this in C++.
I often use OIS in games. It's cross-platform and easy to use. Not sure it's useful for other cases than games but it does the job for you.
The SDL library is one way to do it cross-platform. Here's an example of polling keyboard events.
Put the main routine in a thread, then have something like
static char mode = ' ';
while(mode != 27) // to allow Esc to end program
{
mode = _getch();
}
Threaded code can then do different things based on what key was pressed.
I'm writing simple command-line program in C++ (windows). One functionality of it is to check keyboard input and if a certain key is pressed, exit the program.
Actually it works fine so far, however when the program exits, I get all the pressed key on the output of the command-line? Is there a way to avoid this behavior?
Thanks in advance...
getch has no echo, meaning it does not print the character you type. Here is some more info about all the get char functions in C/C++:
http://www.daniweb.com/forums/thread37195.html
Use _kbhit() to test if a key is pressed. and when it is use _getch() to get the value. (note _getch() can return 0 or 0xE0 for special keys and call _getch() again for that value)
It depends on how you check for keyboard input.
You probably check for the input without actually using it, thus leaving it in the input queue for the next program able to get it (the command line).
You can use getch() which is supposed not to echo the character. I can't remember the header in which it's defined, but I think it was conio.h (remember that it's not a standard header)
I've got a C/C++ program that runs in a console window, and prompts the user to press any key to continue, or 'q' to quit. It currently uses _kbhit() to detect if a key has been pressed, and then uses _getch() to determine what character it was.
Now I want to be able to automate this program (from a C# app using a Process with RedirectStandardInput and CreateNoWindow set). Obviously I can no longer rely on _kbhit() as it uses ReadConsoleInput(), which fails when launched using my C# app. In my C# app I use process.StandardInput.Write("A") to push something onto the stream in an attempt to get my console app to continue.
In the console app I have used SetConsoleMode() to clear the ENABLE_LINE_INPUT flag so that I can use getchar() to return as soon as a character is pressed, and this works reasonably well (when I press a character key in the console window as well as when the call is made from the c# app). However, it has flaws in that it now only accepts characters keys (i.e. no F, Alt, Shift keys etc.) which isn't too big a problem, but moreso I seem to have to press return twice (and this is a key that lots of people will likely choose to press in the 'any key' situation).
Does anyone know how I can make the console app respond to a key (any a bonus, charcters and return only is acceptable) pressed ONCE, whilst still responding to a single character pushed onto the stream from my C# app?
Something I did notice, calling system("PAUSE") gives the exact behaviour I want, other than knowing what key was pressed so that I can quit on 'q'. I don't know how PAUSE does it though, and it doesn't let me use my custom message either :(.
I'm sure there's a really obvious solution, but it has been driving me mad. Any thoughts are much appreciated!
There are two issues with the resolutions:
Neither the C or C++ languages have
portable functions for waiting for a
keyboard key to be hit. It's an
operating system (platform) issue.*
C and C++ have different methods for
resolving I/O. Choose your
language, C or C++.
In my C++ console applications, I ask the user to "Press Enter to Continue" and use the ignore method of cin:
void Pause(void)
{
std::cout << "Press ENTER to continue.\n";
std::cout.flush(); // Insurance, make sure the text is displayed.
std::cin.ignore(100000, '\n'); // Ignore characters until an ENTER (newline) is received.
return;
}
I suggest you create a single file with the Pause function. You can write different versions for different platforms and let the build system select the correct file.
Not all platforms that run C or C++ are required to have keyboards. Many embedded systems don't have keyboards. Also, many windowing systems receive messages, events or signals when a key is pressed. Again, something different and not standard.
Here is a very good implementation for C++. Be sure to read over the entire tutorial, as I it may initially appear that it doesn't help you.
http://www.daniweb.com/forums/thread90228.html
Use cin.get(). This returns the appropriate key.
Thank you for all your responses, I've learned a lot about handling input!
However, I couldn't get anything to work exactly how I wanted, so I had to abandon the unified approach and put in a check to see if it was running in a window. If it is, I stick with _kbhit(), and if not I use PeekNamedPipe(), which gives me the number of characters sent from my C# app without transfering them into the stdin buffer. There's still a few issues that I have to work out to do with clearing any data that I don't want from the pipe, but it's solved my initial problem.
Thanks again for all the suggestions, they will undoubtably come in handy next time I have an input nightmare :)