Since you're using ncurses, you're looking for ungetch()
. From the manpage:
The ungetch routine places ch back onto the input queue to be returned by the next call to wgetch. There is just one input queue for all windows.
The function prototype:
int ungetch(int ch);
It returns the integer ERR
upon failure and an integer value other than ERR
(OK
in the case of ungetch()
) upon successful completion.
For future reference only
If anyone reading this answer wants a simple way to push back characters onto a file stream, ungetc()
is the correct approach. ungetch()
is appropriate only for anyone using ncurses.
ungetc()
pushes back one character into a given file stream:
int ungetc(int c, FILE *stream);
From the manpage:
ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed-back characters will be returned in reverse order; only one pushback is guaranteed.
You will want to call it with stdin
, as in:
ungetc((unsigned char) KEY_UP, stdin);
It returns c
on success, EOF
on error. Bear in mind that it only guarantees one push back character; you can't call ungetc()
twice and then expect that calling getch()
twice gives you back the last 2 pushed characters.