Armenian Knowledge Base  

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

Reply
 
LinkBack Thread Tools
Old 12.07.2002, 08:46   #1
Студент
 
Join Date: 03 2002
Location: [ Surrealistical World ]
Posts: 312
Downloads: 0
Uploads: 0
Reputation: 0 | 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.
Reply With Quote
Old 12.07.2002, 13:20   #2
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Downloads: 0
Uploads: 0
Reputation: 0 | 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
Reply With Quote
Old 12.07.2002, 20:05   #3
Guru Apprentice
 
Join Date: 02 2002
Location: /dev/null
Age: 41
Posts: 524
Downloads: 0
Uploads: 0
Reputation: 0 | 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...
Reply With Quote
Old 13.07.2002, 00:52   #4
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Downloads: 0
Uploads: 0
Reputation: 0 | 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 ).
Reply With Quote
Old 13.07.2002, 01:23   #5
Студент
 
Join Date: 06 2002
Location: Yerevan
Posts: 258
Downloads: 0
Uploads: 0
Reputation: 0 | 0
Post

2 Rust

Usage of ncurses is probably easier and better way of doing it.
Reply With Quote
Sponsored Links
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 08:06.


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