 |
 |
single char input in terminal
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
Hi,
In my CLI-app, I need a routine that waits until a key is pressed and returns its (ASCII-) value. The stdio-routines getchar, fgetc etc. wait for a return key. What can I do? I don't want to invoke the whole CarbonLib to keep overhead small, so I tried to set the stdin-stream to nonbuffering using setvbuf, but it doesn't work.
Thanks,
Daniel
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status:
Offline
|
|
I've been trying for a while to do this, and found nothing in the normal C/C++ libs to do this. I do think you can do it with curses, though. I suggest you look for a tutorial/reference for curses.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
I read some man-pages, and it seems that the termios.h could be the solution. There is the line
#define ICANON 0x00000100 /* canonicalize input lines */
which is described as exactly our problem: The input buffer is not filled until a newline character is received. Maybe you can change it by switching to terminal mode, but I didn't have the time to test it.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Aug 1999
Location: Calgary, Alberta, Canada
Status:
Offline
|
|
I've used getchar() to get single characters from the terminal before. It certainly is possible. I haven't done c/c++ coding for a while though, so I'll have to dig it up 'cause I can't remember how to use that function anymore. 
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
you need to change the input mode on the terminal input descriptor into RAW mode to do that. by default the terminal device driver tries to be 'smart' by buffering and translating characters. using raw mode will makes it forgets it all and forwards the input directly.
have a look at stty(1) and termios(4) to learn about all that crap 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
Thank you! I wonder if there are many people who deal with such low level "crap"  or if this is hopeless old-fashion style.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
well, it's not extraordinarily usefull knowledge, I just picked up at a time where GUIs weren' that common, proves useful now and then :>
serial line discipline and terminal IOs aren't that necessary to know these days, but heck, now and then it still helps
Oh, if you want to do terminal stuff, have a look at curses(3X) it does most of the stuff you might need, and does't depend on terminal type too.
i *still* write terminal-only programs, especialy for administrative stuff; when you are ssh-ing into a box, you end up VERY happy you can still do stuff 
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
Curses(3X) looks nice. Am I right this is to emulate windows in text-only mode? This would be great, just like the good old days...
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
not really *windows* per se (save it's in an implementation of ncurses, but I am not sure the darwin one is that) but allows you to control the cursor and do all kind of stuff without having to worry about the terminal it's displayed on.
It also masks all the terminal input line discipline, like the getkey() problem you have.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
What do you mean by "without having to worry about the terminal it's displayed on". I don't want to run it on any other platform than Mac OS X, inside the Terminal. Isn't it quite a big overhead?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
What happends if you connect from outside into that machine? ssh from a linux box?
And no, trust me, you don't want to have to handle the vt100/ansi sequences, the window-resizing signals and all the rest fo the crap. 10 years ago you'd have been able to make a text BBS with all the knowledge, but now...
Wait, maybe you DO want to make a 2400 bauds text BBS ?

|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
It's a programming language. The normal stdin/stdout-routines would be far enough for me, but this weird input-line-assembling is in my way.
In all the termios-functions, you need a 'struct termios' or in the tty-functions, it's a 'struct tty'. How do I access these? I thought all you got was stdin/out/err.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
of course not. stdin/out/err are just FILE wrappers around the 3 *real* low level 'file descriptors' (0 = stdin, 1= stdout, 2=stderr)
since they are file descriptors, you can do all kind of stuff with them, even file operations, non-buffered read() and all that.
it's the 'fd' parameter of tcgetattr() for example.
note that MOST of the crap is now obsolete, it applied to serial lines (speed, etc etc) and is just emulated for the 'virtual tty' that terminal uses.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2000
Status:
Offline
|
|
Well, thank you very much, BusErrorDev! If you say that all this crap (you like this word, don't you?  ) is just emulated, is there any modern way to do it? The terminal is just perfect for me now, but if I could to text I/O with less old-style overhead, this would be even better. But for now I'm happy
Finally, it works, I even found some nice sample source code at csene.org
<font face = "courier"> #include <termios.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
struct termios orig, changes;
tcgetattr(STDIN_FILENO, &orig);
changes = orig;
changes.c_lflag &= ~(ICANON|ECHO);
tcsetattr(STDIN_FILENO, TCSADRAIN, &changes);
puts("Hit any key!");
getchar();
puts("Thanks!");
/* restore old settings */
tcsetattr(STDIN_FILENO, TCSADRAIN, &orig);
return 0;
} </font>
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|