cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Keyboard inputs

Sudhakar.S
Occasional Contributor

Reading Keyboard inputs

Can i give input to Gnome-terminal using programs(in C or Java)?.

If i give the input to one Gnome-terminal like ls,hostname and etc.How can i get the commands from the terminal in my program?.
2 REPLIES
Steven E. Protter
Exalted Contributor

Re: Reading Keyboard inputs

A terminal is a terminal.

stdin is the keyboard. That should do it.

The read command stops the program nicely and waits for input followed by a return.

That should be enough unless you want to script it.

while read -r aa
do
echo $aa
done < input_file

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Mark Grant
Honored Contributor

Re: Reading Keyboard inputs

However, if you want to use C it all gets a little more complicated. You can use the standard C routines such as getc and scanf and all that but they are all a little disappointing because linux, like unix, will not give you anything until the "enter" key is pressed.

All is not lost, however, because you can change this behavior by changing the terminal into what is know as "raw" mode. In other words, characters are "unbuffered" which basically means your "getc()" works as you would expect. Sadly, this can drastically slow the application down if you don't handle it correctly. Single character at a time reads, are horribley expensive when you do lots and lots of them so use functions that read specified number of characters when you can.

For all of this, there is no useful "man" page that I have ever found but a quick google search turned up the following link which might be usefull.
http://linux.cis.nctu.edu.tw/docs/woven/HOWTO/Serial-Programming-HOWTO-2.html

If all this sounds all a bit horrid have a look at the "curses" package which does most of the nasty stuff for you and makes doing cursor positioning stuff a lot easier too.

Sorry, can't help you on the Java front though.
Never preceed any demonstration with anything more predictive than "watch this"