Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single Key input (ie a C "getch" replacement)

Status
Not open for further replies.

hilme

Programmer
Sep 12, 2001
2
GB
hi,

I'm trying to input single keys from the terminal on a linux system into Tcl. I have tried various arrangements of gets and read (specifiying I want 1 character etc) and setting the buffering to none (_output_ buffering I now realise!) but still have to press return at the end of the string to get tcl to read anything at all.

How can I get it to accept a single key press as input and continue without needing to press return?

Thanks,

Chris.
 
You can use bind to capture any keypress like this:

bind . <Any-KeyPress> { your_proc %K }

This will pass the procedure &quot;your_proc&quot; the key that was pressed. The stock example is this:

bind . <Any-KeyPress> { puts &quot;Keysym is %K&quot; }

Good luck

Jeff.
 
If you're talking about terminal input from the console (or an Xterm), this isn't a Tcl issue as such. It's an issue of the terminal being in &quot;cooked&quot; mode. In cooked mode, the Unix terminal driver does line buffering, which means that it waits until the user presses the Enter key before sending the entire line to the application. This is usually a Good Thing, in that the terminal driver takes care of handling line editing (such as processing Backspace keys, etc.), greatly simplifying your application I/O code.

If you really want your application to handle every single keystroke, you'll need to set the terminal driver to &quot;raw&quot; mode using the Unix stty command. Use the Tcl exec command to run stty with options appropriate for your system. Then I think you'll be able to read character by character with the read command as you described. But overall, it's messy business. Typically, I've messed around with stty settings only when using Expect, and then only as little as possible. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thats great, it all works fine now. Thank you very much. Chris.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top