Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters

Reply
 
Thread Tools

Linux Console Programming II
Old 12.07.2002, 07:46   #1
Студент
 
Join Date: 03 2002
Location: [ Surrealistical World ]
Posts: 312
Rep Power: 0
Post Linux Console Programming II

GETCH.

How do I write programs which are handling keyboard keypress events? In DOS we were using getch(); and getche(); functions. In Linux there is also a getch(); function, this time in curses.h (in DOS it was in conio.h), getch() calls wgetch(STDSCR); - I write the code just the way it is explained in the manual, but it gives error messages... [about wrong references...]. What's up?
Thanks anyway.
__________________
::Against the crowd.

Old 12.07.2002, 12:20   #2
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Rep Power: 0
Post

_getch is not ANSI, so just forget it.

This may help - http://www.eskimo.com/~scs/C-faq/q19.1.html

e.g. of turning to raw mode:
Code:
 
char *save_stty()
{
FILE *pp;
static char save_buf[256];

pp = popen (&quot;stty -g </dev/tty&quot;);
fgets(save_buf, sizeof save_buf, pp);
save_buf[strlen(save_buf - 1)] = '\0';    /* strip nl */
pclose(pp);
return save_buf;
}

void set_raw_stty()
{
system(&quot;stty raw -echo </dev/tty&quot;);
}

void reset_stty(char *saved_settings)
{
char *sysbuf[512];
sprintf (sysbuf, &quot;stty %s </dev/tty&quot;, saved_settings);
system(sysbuf);
}
Or instead just use ncurses library - http://www.gnu.org/software/ncurses/ that will give you the solution for both colors, user input etc...
__________________
http://www.d-brane.com

Old 12.07.2002, 19:05   #3
Guru Apprentice
 
Join Date: 02 2002
Location: /dev/null
Age: 50
Posts: 524
Rep Power: 0
Post

to Eddi: why do you want to do it in "raw mode"???
Why makes things complicated when they are not!!!?

to RUST:

In Linux all input-output operations done through streams. Your program will have by default 3 stremas (input, output and error).
If you writing in c++ - names are cin, cout and cerr.
In C - there are 3 files: stdin, stdout and stderr.

they alerady opened by fopen, so all you need is just getc(stdin)
or getchar()
__________________
\/\/h47'5 1n 4 n4m3? 7h47 wh1(h w3 (4|| 4 r053,
8y 4ny 07h3r n4m3 w0u|d 5m3|| 45 5w337...

Old 12.07.2002, 23:52   #4
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Rep Power: 0
Post

Quote:
Originally posted by Ektich:
to Eddi: why do you want to do it in "raw mode"???
Why makes things complicated when they are not!!!?

to RUST:

In Linux all input-output operations done through streams. Your program will have by default 3 stremas (input, output and error).
If you writing in c++ - names are cin, cout and cerr.
In C - there are 3 files: stdin, stdout and stderr.

they alerady opened by fopen, so all you need is just getc(stdin)
or getchar()
The thing is that as far as I understand he doesn't want stream read! He wants to react to key pressing, getchar doesn't have that. Even more, ANSI doesn't know about keyboard.

And that's the reason to change to raw mode ( where getchar will return immediately, without waiting till enter ).

Old 13.07.2002, 00:23   #5
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Rep Power: 0
Post

2 Rust

Usage of ncurses is probably easier and better way of doing it.
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 13:49.
Top

Powered by vBulletin® Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.