If you use getc()
or scanf()
, you only know what button the user pressed after he pressed enter. Is there a way to get direct keyboard input in the C language?
Also, I'm using gcc on Linux (Raspberry pi).
If you use getc()
or scanf()
, you only know what button the user pressed after he pressed enter. Is there a way to get direct keyboard input in the C language?
Also, I'm using gcc on Linux (Raspberry pi).
It is OS specific (the C99 or C11 standard does not know about the keyboard). On Linux (which I am focusing on in my answer) and Unix, terminals are -for legacy and historical reasons- complex beasts (the kernel is implementing the line discipline), see tty demystified page. For gory details, see also termios(3) (the raw and noncanonical modes will give you a headache) & tty(4) & stty(1)
(you could spend months in learning the gory details; I forgot most of them)
I strongly recommend coding with a library such as ncurses or readline (or termcap, if you are masochistic enough)
If using a GUI, better use some toolkit like Qt.
To poll a set of file descriptors, use poll(2).
See also syscalls(2) and Advanced Linux Programming.
Normally the terminal is in canonical state, which means the key events are only passed to the program after pressing Enter
. For immediate keyboard response we need raw mode.
This post may help.
More on raw mode.