getch()
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hello,
Is there any way to get a character without pressing return in a simple
console application ( something like getch() of ncurses ) ? It's needed in a test shell for getting the arrow keypresses for using the history list. TIA.
Khoda Hafez
Progga
Quoted 4 lines · Progga
Is there any way to get a character without pressing return in a simple
console application ( something likegetch()of ncurses ) ? It's needed in a test shell for getting the arrow keypresses for using the history list. TIA.
Yes. Test it before, it is old code...
Using the code below, I had a loop (it is from a console game I was trying to write 4-5 years before):
while (ch! = 'q') {
fflush (stdin);
if (read (STDIN_FILENO, &ch, 1) > 0)
if (ch >= _UP && ch <= _LEFT)
{
check_move (ch);
...
}
....
....
} And, what was _UP and _LEFT? Hmm...
[anteater@neutrino berserkers]$ grep _UP *.h
#define _UP 65
[anteater@neutrino berserkers]$ grep _LEFT *.h
#define TOP_LEFT 51
#define BOT_LEFT 52
#define _LEFT 68
Elias
--CUT HERE--
/*
* Changing Terminal attributes using low level terminal handling,
* in order to have non-buffered, non-echoed, non-blocking input. *
* */
#include <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
struct termios saved_attrib;
void reset_input_mode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &saved_attrib);
}void set_input_mode(void)
{
struct termios attrib;
char *name;
/* check stdin is a terminal */
if (!isatty(STDIN_FILENO)) {
fprintf(stderr, "Not a terminal!\n");
exit(EXIT_FAILURE);
}
/* save original attributes */
tcgetattr(STDIN_FILENO, &saved_attrib);
atexit(reset_input_mode);
/* make the changes */
tcgetattr(STDIN_FILENO, &attrib);
attrib.c_lflag &= ~(ICANON|ECHO); /* disables canonical mode, echo */
attrib.c_cc[VMIN] = 0; /* non-blocking input */
attrib.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &attrib);
}Quoted 9 lines · Elias Athanasopoulos
Quoted 4 lines
Is there any way to get a character without pressing return in a simple
console application ( something likegetch()of ncurses ) ? It's needed in a test shell for getting the arrow keypresses for using the history list. TIA.Yes. Test it before, it is old code...
Using the code below, I had a loop (it is from a console game I was trying to write 4-5 years before):
This is basically the right approach (i.e. use tcsetattr() to disable canonical mode).
However, I'll just mention a couple of other points:
Quoted 3 lines
while (ch! = 'q') {
fflush (stdin);fflush() is only applicable to output streams; using it on input streams seems to be a common programming "superstition".
Quoted 8 lines
And, what was
_UPand_LEFT? Hmm...[anteater@neutrino berserkers]$ grep
_UP*.h
#define_UP65
[anteater@neutrino berserkers]$ grep_LEFT*.h
#defineTOP_LEFT51
#defineBOT_LEFT52
#define_LEFT68
IOW, 'A', 'B', 'C', 'D'. I doubt that you were actually using those keys; more likely, assuming that the cursor keys generated particular sequences and ignoring most of the sequence.
If you want to use extended keys (those which generate sequences rather than individual codes, e.g. cursor keys, function keys, numeric keypad, etc), use termcap/terminfo. Or just use curses; when someone wants getch(), they often want the other stuff which curses provides.
Quoted 6 lines
struct termios
saved_attrib;void
reset_input_mode(void){ tcsetattr(STDIN_FILENO, TCSANOW, &saved_attrib); }
[snip]
Quoted 1 line
atexit(reset_input_mode);
To be safe, you should also install signal handlers which restore the terminal mode.
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 2 lines · Glynn Clements
fflush()is only applicable to output streams; using it on input streams seems to be a common programming "superstition".
Correct. I really can't tell why this was there. :-) (I can't also tell why I didn't see the bogocity while copy-pasting it...)
Quoted 8 lines
Quoted 4 lines
[anteater@neutrino berserkers]$ grep
_LEFT*.h
#defineTOP_LEFT51
#defineBOT_LEFT52
#define_LEFT68IOW, 'A', 'B', 'C', 'D'. I doubt that you were actually using those keys; more likely, assuming that the cursor keys generated particular sequences and ignoring most of the sequence.
Yes. IIRC, each arrow kay had a sequence of 3 codes. I was matching only the first one, because otherwise (i.e. by matching each one of the 3 codes) the game was too slow. :-)
Elias
Quoted 6 lines
tcgetattr(STDIN_FILENO, &attrib); attrib.c_lflag &= ~(ICANON|ECHO); /* disables canonical mode, echo */ attrib.c_cc[VMIN] = 0; /* non-blocking input */ attrib.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSAFLUSH, &attrib);
Thanks all for the super suggestions. I only had to change one line for
getting what I need :
Quoted 1 line
attrib.c_cc[VMIN] = 0; /* non-blocking input */
to
attrib.c_cc[VMIN] = 1; /* blocking input */
If input is taken in non-blocking mode then something like:
while( read( STDIN_FILENO, &ch, 1 ) != 1 ) ;
needs to be used. It's also OK. But to use getchar()/cin, using blocking
mode appeared the most suitable. So I did the change. The chance to control the echo and nonecho mode is another beauty. I needed the nonecho and it was already in the code. Thanks again from me and my friend Mahbub.
Khoda Hafez
Progga
Quoted 5 lines · Progga
If input is taken in non-blocking mode then something like:
while( read( STDIN_FILENO, &ch, 1 ) != 1 ) ;needs to be used.
NO! This will result in a "busy wait", consuming all available CPU time.
If you want to wait until a byte is actually available to be read, either:
a) don't use non-blocking mode in the first place,
b) temporarily disable non-blocking mode, or
c) use select() to wait until input is available.
Signature · 1 line
Glynn Clements <[email protected]>
defining a #define
In reply to a message not in the archiveif i compile a library with a certain build flag there are times when i would like to reflect that flag into the header file for the library, so that larger compilations involving the library are built the same way.
ie. something like
---library.h---
#ifdef BUILD_THIS_WAY
#text "#define BUILD_THIS_WAY"
#endif
i could not find anything in the gnu cpp docs. but does anyone know for sure there *is* a way?
thanks.
- chris
Re: defining a #define
Just what you did is ok
#ifdef LINUX
#define GREAT_API
#define OS "Linux"
#else
#define UNKNOWN
#endif
Christopher Quinn wrote:
Quoted 50 lines · Christopher Quinn
if i compile a library with a certain build flag there are times when i would like to reflect that flag into the header file for the library, so that larger compilations involving the library are built the same way.
ie. something like
---library.h---
#ifdefBUILD_THIS_WAY
#text "#defineBUILD_THIS_WAY"
#endifi could not find anything in the gnu cpp docs.
but does anyone know for sure there *is* a way?thanks.
- chris-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.htmlif i compile a library with a certain build flag there are times when i would like to reflect that flag into the header file for the library, so that larger compilations involving the library are built the same way.
ie. something like
---library.h---
#ifdefBUILD_THIS_WAY
#text "#defineBUILD_THIS_WAY"
#endifi could not find anything in the gnu cpp docs.
but does anyone know for sure there *is* a way?thanks.
- chris-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: defining a #define
In reply to a message not in the archivesorry - i wish i could withdraw that question, as it was ridiculous!
thanks.
- chris
Quoted 9 lines · Miguel Griffa
Just what you did is ok
#ifdef LINUX
#defineGREAT_API
#define OS "Linux"
#else
#define UNKNOWN
#endif