LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2002, 06:06 AM   #1
lea
Member
 
Registered: Jul 2002
Posts: 31

Rep: Reputation: 15
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
 
Old 11-25-2002, 10:58 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
Old 11-27-2002, 05:29 AM   #3
lea
Member
 
Registered: Jul 2002
Posts: 31

Original Poster
Rep: Reputation: 15
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
 
Old 09-07-2003, 03:41 PM   #4
kc8tbe
Member
 
Registered: Feb 2003
Location: Cleveland, Ohio (USA)
Distribution: Gentoo, Kubuntu 6.06
Posts: 179

Rep: Reputation: 30
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!

Last edited by kc8tbe; 09-07-2003 at 04:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i scan a character from keyboard without the user having to press enter rasika Programming 6 02-11-2010 08:54 PM
Statistics from stdin? edeca Linux - Software 0 05-23-2005 10:03 AM
stdin and NUL. How ? Mike Davies Linux - Software 5 10-28-2004 07:06 AM
"Disk boot failure. Enter system disk and press enter" Fear58 Linux - Hardware 3 06-25-2004 05:17 PM
unbuffered stdin mvt Programming 7 05-17-2004 09:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration