Armenian Knowledge Base

Armenian Knowledge Base (https://forum.armkb.com/)
-   Languages, Compilers, Interpreters (https://forum.armkb.com/languages-compilers-interpreters/)
-   -   Linux Console Programming II (https://forum.armkb.com/languages-compilers-interpreters/29-linux-console-programming-ii.html)

Rust 12.07.2002 07:46

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.

Eddi 12.07.2002 12:20

_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...

Ektich 12.07.2002 19:05

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() ;)

Eddi 12.07.2002 23:52

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 ).

Eddi 13.07.2002 00:23

2 Rust

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


All times are GMT. The time now is 04:02.

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