I'm currently developing various console games in Windows that won't really work using regular input via cin.
How can I (In a simple way using only standard windows libraries available in MSVC):
Make the program wait for a (specific?) key press and return the key ID (It would have to work for all keys including the arrow keys)
During a real-time game check for the last pressed key of the user and if there was any key pressed since the last check.
It would really help if you could include a short example program for your solution
I've got just what you need.
Here enjoy pal:
C++ source
It's pretty much self-explanatory but if you have any doubts my email is jacobossm#gmail.com
AFAIK you can't do it using the standard C runtime. You will need to use something such as the Win32 function GetAsyncKeyState.
You want the Windows Console API, for example PeekConsoleInput.
Related
Im writing a little C++ progam but got to a point from which on i could not go on:
I want my program to control an external program using a certain combination of keys.
Yes, this sounds like a horribly unclean workaround, but thats what i want to do ;)
To be more specific:
I want my little C++ program to control the already running program elinks (a text based internet browser) by pressing the "esc" then "v" and then "h" -keys (this is used to toggle between html and plain text output in elinks).
But unfortunately, I do not know how to get a C++ program to type a certain combination of keys (...if this is possible at all).
I already tried the search function but wasnt able to find a solution.
It would be very kind of you, if someone could give me a hint on what to do here.
Thanks,
Spoekenkieker
P.S.: Im running Linux
If you have control over how to start the elinks browser, you can start it under the GNU screen terminal multiplexer and use its stuff command to send key inputs:
https://unix.stackexchange.com/questions/13953/sending-text-input-to-a-detached-screen
This way you're not required to learn the complications of X Windows programming.
I need to disable the task keys for both Linux and windows using a C program . I tried using the windows.h but as the name states it is not working for the Linux and in case of windows also its not working properly . I tried to do this using the GetAsyncKeyState function but still have no clues to Linux key handling. As I am new to system code I have referred to following but unable to get through the issue.
So please suggest some solution that could be helpful in handling keys (Enable/Disable) on both platforms (Linux/Windows) ?
And is it okay to use key-scan codes and ASCII codes for the Key event handling?
I have already referred to :
Disable task switching keys with c++
Disable keyboard keys when the console of c Run using c or c++
How to handle key press events in c++
If you are looking for low level, cross platform keyboard handling then you might want to have a look at libsdl - http://www.libsdl.org/. The keyboard handling section is - http://wiki.libsdl.org/CategoryKeyboard
I am writing an application for a robot.
The required UI for the application is described in the pseudo-code below:
while(true){
if (spacebar is not pressed){
//do something
}
else{
sleep(1); //wait for a second
}
}
If I use cin or some other console input reading function then it will wait for user to press something. How do I ensure that it does not wait to get any input?
I am using Ubuntu. But I do not want it to be OS-specific.
Answers here seem to be OS specific.
Terminal Level input
What you are asking for is fairly close to the hardware (key-press / key-release) compared to the "standard input/output" stream concepts. So your implementation would have to be OS specific. Having said that the library to use is curses[1] which has been around for a long time and is standard on a lot of Un*x platforms. The GNU ncurses flavor compiles for pretty much all of them, it is a standard install in almost all Linux environments, and where it isn't installed by default you can find it. It also works well in Windows (cygwin), os/2 and a bunch of embedded systems etc. so you should be able to write a fairly portable software using curses that does what you want.
It's not too clear what you're asking for. In the if, is the
condition based on whether a space character has been entered,
or whether the user is currently holding down the space bar? In
the first case, you need something like curses (a portable
library which does gets each character as it was entered,
without echo). In the second, I don't think that there is
a portable solution. Even non-portably, you might not be able
to get it if your program is reading from a terminal window
(e.g. xterm); this sort of information is typically only present
as window events, when you created the window in your program.
I have a homework assignment to create a game. So I have created a game that generates random numbers, displays them, then after a few seconds the numbers disappear and the user has to enter them in descending order. The game works fine and all the output is correct... but the problem is I have created different functions for creating numbers, arranging them, accept.. and I have to press the enter key a few time each time a function call comes.
Is there a way by which these functions get called without pressing any key to make the program work without pressing the enter key?
Pulled from Capture characters from standard input without waiting for enter to be pressed
That's not possible portably in pure C++, because it depends too much
on the terminal used that may be connected with stdin (they are
usually line buffered). You can, however use a library for that:
conio available with windows compilers. Use the function _getch() to
give you a character without waiting for the enter key. I'm not a
frequent windows developer, but i've seen my classmates just include
conio.h and use it. See conio.h at wikipedia. It lists getch, which
is declared deprecated in Visual C++.
curses available for linux, compatible curses implementations are
available for windows too. It has also a getch function. (try man
getch to view its manpage). See Curses at wikipedia.
I would
recommend you to use curses if you aim for cross platform
compatibility. That said, I'm sure there are functions that you can
use to switch off line buffering (i believe that's called "raw
mode", as opposed to "cooked mode" (look into man stty)). Curses
would handle that for you in a portable manner if i'm not mistaken.
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 :)