no conio library so not getch()

I am learning C++ on a Raspberry Pi with Code:Block. There is no library with the name "conio" so there is not getch() function.

Is there another function for getch() that can be used?
Something like this:

 
char get = std::cin.get();
ncurses has that function.

std::cin.get(); only works with pressing enter.
Anyone ever been able to do this in raw C++ ? I tried and never got it. I can do it in assembly block wrapped up in c++, but not anything clean. I suspect however ncurses did it is not pure c++ either? I tried to do it with peeks and clears and all, and it may be doable, I just lost focus on it as I was playing around.
Last edited on
"raw C++" meaning no OS API calls? Probably not. As I'm sure you know, C++ doesn't know what "enter" or a key press means, that's the job of the terminal. C++ just receives the data it's given.

But speaking of ncurses, the source is: https://ftp.gnu.org/pub/gnu/ncurses/
If you download the source, you'll see
./doc/hackguide.doc: All ncurses input funnels through the function wgetch(), defined in ...
./doc/html/hackguide.html: <code>wgetch()</code>, defined in <code>lib_getch.c</code>. This

lib_getch.c shows wgetch as defined, located at: ncurses\base\lib_getch.c
wgetch calls _nc_wgetch... which calls other functions, include PTHREADS-related calls. I'd imagine at some point it will make an OS-specific API call.

From this page: https://www.instructables.com/id/Controlling-a-Raspberry-Pi-RC-Car-With-a-Keyboard/
getch (in python) is defined as:
1
2
3
4
5
6
7
8
9
10
11
12
# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


The same code could probably be translated into C++. But certainly "termios" is not "pure C++", it's some library call.
http://man7.org/linux/man-pages/man3/termios.3.html
Last edited on
I would take something off color that was portable too, but no OS or CPU specifics.
Maybe I will pick that up again... or at least see where I left it and such.

looks like readsome can do it for some compilers, but not all.
and I found a 3 page threaded one that was exceedingly complicated.

no joy in Cygwin/g++ yet. changing stdin params or setting it to be used by cin didn't help. readsome didn't work. Peeks didn't work. Simplest thing is a royal pain if your compiler (or is it the OS) doesn't want you to do it.
Last edited on
Topic archived. No new replies allowed.