LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   get character without enter/stdin-buffer (https://www.linuxquestions.org/questions/programming-9/get-character-without-enter-stdin-buffer-36402/)

lea 11-25-2002 06:06 AM

get character without enter/stdin-buffer
 
Hi all!

Sorry for asking a simple question.

I want to input a single character without enter (like getchar() does it).

Conio.h (with _getch()) seems to be a Turbo-C-Include.

Is there a (simple) alternative to curses/ncurses (which I used to use)? - My aim is it to produce small binaries.

TIA
Lea

crabboy 11-25-2002 10:58 AM

It's the terminal that requires you to press enter and not getchar(). You need to modify the behavior of you terminal and turn off canonical mode . Take a look at my post (the last one) in this thread, it should answer your question:

http://www.linuxquestions.org/questi...enter+terminal

lea 11-27-2002 05:29 AM

Thanks crabboy

I solved it your "easy way" with a system call
system("stty -icanon -echo");
and before leaving the program
system("stty icanon echo");

Lea

kc8tbe 09-07-2003 03:41 PM

Wow, that stty command really helps!
A curses alternative I find useful is the ANSI driver. For example, outputing "Esc[30m" to the terminal changes the foreground color to black. I've written all the ANSI driver calls I know of in macro form:
Code:

#define cursor_moveyx(y,x) printf("\033[%d;%dH",y,x)        /*Move cursor to position y,x (rows, columns) with (1,1) as origin*/
#define cursor_moveup(y) printf("\033[%dA",y)                /*Move cursor up y*/
#define cursor_movedown(y) printf("\033[%dB",y)                /*Move cursor down y*/
#define cursor_moveright(x) printf("\033[%dC",x)        /*Move cursor right x*/
#define cursor_moveleft(x) printf("\033[%dD",x)                /*Move cursor left x*/
#define cursor_store() printf("\033[s")                        /*Store current cursor position and color*/
#define cursor_restore() printf("\033[u")                /*Restore cursor position and color from cursor_store()*/
#define cursor_clear()        printf("\033[2J")                /*Clear screen and leave cursor where is*/
#define cursor_clearline() printf("\033[K")                /*Clear to end of line and leave cursor where is*/
#define cursor_fore_black() printf("\033[30m")                /*Change foreground color to black*/
#define cursor_fore_red() printf("\033[31m")                /*Change foreground color to red*/
#define cursor_fore_green() printf("\033[32m")                /*Change foreground color to green*/
#define cursor_fore_orange() printf("\033[33m")                /*Change foreground color to orange*/
#define cursor_fore_blue() printf("\033[34m")                /*Change foreground color to blue*/
#define cursor_fore_magenta() printf("\033[35m")        /*Change foreground color to magenta*/
#define cursor_fore_cyan() printf("\033[36m")                /*Change foreground color to cyan*/
#define cursor_fore_white() printf("\033[37m")                /*Change foreground color to white*/
#define cursor_back_black() printf("\033[40m")                /*Change background color to black*/
#define cursor_back_red() printf("\033[41m")                /*Change background color to red*/
#define cursor_back_green() printf("\033[42m")                /*Change background color to green*/
#define cursor_back_orange() printf("\033[43m")                /*Change background color to orange*/
#define cursor_back_blue() printf("\033[44m")                /*Change background color to blue*/
#define cursor_back_magenta() printf("\033[45m")        /*Change background color to magenta*/
#define cursor_back_cyan() printf("\033[46m")                /*Change background color to cyan*/
#define cursor_back_white() printf("\033[47m")                /*Change background color to white*/
#define cursor_attr_none() printf("\033[0m")                /*Turn off all cursor attributes*/
#define cursor_attr_bold() printf("\033[1m")                /*Make test bold*/
#define cursor_attr_underline() printf("\033[4m")        /*Underline text*/
#define cursor_attr_blink() printf("\033[5m")                /*Supposed to make text blink, usually bolds it instead*/
#define cursor_attr_reverse() printf("\033[7m")                /*Swap background and foreground colors*/

These macros perform a lot of the basic functions of curses and they are the ANSI standard and should therefore be portable to most systems. Since they are macros, putting them in your program won't increase binary size much and won't slow down execution. Just be sure to include stdio.h for them to work!


All times are GMT -5. The time now is 07:55 PM.