I have been doing some reading, and I see that I can use getch() to get a keystroke. What I have seen is that this is considered bad practice, however I have seen conflicting opinions. I am writing a console application for my class, and would like to be able to move a marker(*) around the screen based on the arrow keys being pressed. Is getch() the right way to go about this, or is there a better method to capture it. I want them to just be able to push the arrow, not need to push enter or anything. I don't need the code specifically, I just want to know if I should avoid getch(), and if so, what functions are there for this type of idea.
Asked
Active
Viewed 5,098 times
4 Answers
1
getch()
is not a standard function in either C or C++. It's found in some obsolete compilers, such as Turbo C and it's also defined in certain commonly used libraries such as curses, but in any case it's a C function, not C++. For C++ you should probably just stick with standard C++ I/O. If you can't do this for some reason then go for the most portable option, e.g. curses.

Paul R
- 208,748
- 37
- 389
- 560
-
is there s c++ standard way to capture keystrokes as they are typed, or does c++ always need the input to be buffered? – Bear Nov 18 '10 at 21:09
-
@bear: no there is no such way in standard c++ – smerlin Nov 18 '10 at 21:11
-
I think any solution is either going to be OS-specific, or is going to require a library such as curses or ncurses. – Paul R Nov 18 '10 at 21:13
1
You want to read from the terminal in non-canonical mode. Use tcsetattr() to turn off the ICANON flag.

John Gordon
- 2,576
- 3
- 24
- 29
0
On Windows you can use pdcurses: http://pdcurses.sourceforge.net/, that is compatible with ncurses.

Luis G. Costantini R.
- 1,028
- 7
- 6