I am using C and I am trying to write a small game. But I need to read the input whenever the player tabs on a key (say s), and the program does something, without pressing the enter.
I have seen many posts answering this, but they all use getchar()
or getch()
from curses or conio.h
in Windows. But that's not what I want. The result can definitely done because of 2 examples:
vim
when the user tabs on the :, it will immediately turns into the command mode to let you type in the commands.- the
read
command in bash, whose-n${number_you_want}
parameter can limit the chars it reads without enter.
But I wonder how they can do this even I already have the source of the functions in the examples. I still can't figure out how they do this.
The source code of vim
's inchar()
from getchar.c
and the read
command from bash source read.def
.
Can anyone help me explain how they works? Thanks in advance.