I want to detect when the spacebar is pressed in a CMD window without having to go through cin, because as far as I know cin doesn't return any input until it detects a newline. Am I wrong about that? Can cin capture a single keystroke without a newline?
4 replies to this topic
#1
Posted 04 September 2010 - 04:52 PM
|
|
|
#2
Posted 04 September 2010 - 06:40 PM
MSDOS (Windows) provides conio.h to handle character input, using the functions getch or _getch. An example from MSDN (http://msdn.microsof...4(VS.60).aspx):
/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/
#include <conio.h>
#include <ctype.h>
void main( void )
{
int ch;
_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );
_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 04 September 2010 - 09:14 PM
I managed to find some information on ncurses which was suggesting that conio was "behind the times" and the curses and ncurses are a bit more functional as they cater to more up-to-date stuff. Is that not the case? Does ncurses not work with Windows and should I use conio then?
#4
Posted 04 September 2010 - 10:05 PM
ncurses can work under POSIX (or gnu32win packages) but is not a native Windows API, conio is old (_ prepended to functions are >1989) but it is your best choice as long as your compiler supports it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 05 September 2010 - 11:35 PM
cin reads only an input and before you don't put a newline it does not put it into input. implement your own input handler. use getch (or getche if you want to show pressed key) and kbhit and make it work as you wish
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









