I need a getch() - Any help?

I need a getch() - Any help?

Post by Brian Flemin » Mon, 26 Jan 1998 04:00:00



Hi, I'm programming in DOS, but I need to run my program on the school UNIX
system.  I am using the gcc compiler.

Anyway, the UNIX system doesn't have conio.h, which has the getch() function
that I use in DOS to pause the program until the user presses a key.

Is there any SIMPLE, easy, QUICK, already-made, function in UNIX that will
do this for me?  Someone mentioned curses.h will have getch(), but I'm
getting errors involving stdscr, and wgetch... I really don't get it.
Frankly, I am not familiar with UNIX, and I am definately slow at getting
around the text-based OS.

Thanks for any help.

 
 
 

I need a getch() - Any help?

Post by Dan Kege » Mon, 26 Jan 1998 04:00:00



> Anyway, the UNIX system doesn't have conio.h, which has the getch() function
> that I use in DOS to pause the program until the user presses a key.

> Is there any SIMPLE, easy, QUICK, already-made, function in UNIX that will
> do this for me?

Get the module raw.c / raw.h from
http://www.alumni.caltech.edu/~dank/uwho/uwho218b.zip
- Dan

--
Reply to dank at alumni.caltech.edu

The opinions expressed in this message are my own,
and are not the opinions of my employer.

Charles Darwin - a guy with a Vastly dangerous idea!

 
 
 

I need a getch() - Any help?

Post by UChin Ki » Mon, 26 Jan 1998 04:00:00



> Hi, I'm programming in DOS, but I need to run my program on the school UNIX
> system.  I am using the gcc compiler.

> Anyway, the UNIX system doesn't have conio.h, which has the getch() function
> that I use in DOS to pause the program until the user presses a key.

> Is there any SIMPLE, easy, QUICK, already-made, function in UNIX that will
> do this for me?  Someone mentioned curses.h will have getch(), but I'm
> getting errors involving stdscr, and wgetch... I really don't get it.
> Frankly, I am not familiar with UNIX, and I am definately slow at getting
> around the text-based OS.

> Thanks for any help.

Hi,

Check out the following documentation:


--


 
 
 

I need a getch() - Any help?

Post by Tjerk Santegoe » Tue, 27 Jan 1998 04:00:00


:Anyway, the UNIX system doesn't have conio.h, which has the getch() function
:that I use in DOS to pause the program until the user presses a key.
:
:Is there any SIMPLE, easy, QUICK, already-made, function in UNIX that will
:do this for me?  Someone mentioned curses.h will have getch(), but I'm
:getting errors involving stdscr, and wgetch... I really don't get it.

Ok... I don't know much about curses but I've tried the following example
and seems to (more or less) do what you want.

Compile it with:  gcc foo.c -lncurses

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>

int main( void )
{

        initscr() ;  /* Inititialize the routines */
        cbreak() ;   /* Disable line buffering */
        noecho() ;   /* Don't echo characters back to the screen */
        getch() ;    /* Wait until keypress */
        endwin() ;   /* Restore tty mode */

        exit( 0 ) ;

Quote:}

Greetings,
                Tjerk.

--
Tjerk Santegoeds
Apeldoorn - The Netherlands

 
 
 

I need a getch() - Any help?

Post by Daniel LaBel » Wed, 04 Feb 1998 04:00:00




) Newsgroups: comp.unix.programmer
) Date: Sun, 25 Jan 1998 15:51:15 -0600

)
) Hi, I'm programming in DOS, but I need to run my program on the school UNIX
) system.  I am using the gcc compiler.
)
) Anyway, the UNIX system doesn't have conio.h, which has the getch() function
) that I use in DOS to pause the program until the user presses a key.
)
) Is there any SIMPLE, easy, QUICK, already-made, function in UNIX that will
No, but I think the getch() below is equiv. Should only need to paste it in,
with necessary includes.

) do this for me?  Someone mentioned curses.h will have getch(), but I'm
) getting errors involving stdscr, and wgetch... I really don't get it.

Curses is an all or nothing proposition, I'd say.
You should not mix stdio with curses, so converting to curses might
be more work than you want.
I thought something like this might work:
 int main()
  {
    initscr();

    ...

    getch();

    ..

    endwin();
    return 0;
  }

According to the O'reilly book I have, _Programming_with_Curses_ ,
getch() will set and restore modes if it needs to.  
So. initscr() to satisfy curses.. then ignore it ,basically and use
c-style-io , calling getch() for the occasional pause.
However, this didn't work. (on my system at least)
It's kind of a dirty hack anyway, so.. no real loss.

) Frankly, I am not familiar with UNIX, and I am definately slow at getting
) around the text-based OS.
)
) Thanks for any help.
)
)
)
There is a libconio for Linux which might to the trick, if you run that
unix is linux..  From what I read about it, libconio is a wrapper around
ncurses so it /might/ work on other systems too.
I did remember reading that its purpose was for easy porting of programs
that use std-c and conio to linux.
However, if you'r not familiar with Unix then getting and installing
a lib ( and perhaps tweaking things to get it compile ) probably won't help
you.  

Having said all of this, I think I do have a solution for you.
I had a somewhat similiar problem, except that I wanted
a function that returns the char, or something else if no key is pending.
So I can do something like:
  while ( !getchp() ) rand();
  x=rand();

Actually, answering your post made me functionlize things, and cleared
up a few things for me. Anyway, here's the code.

Anyone can do what they want with it (PD).. NO warranty, etc, etc.

---------------------------------------------------------------
/* Daniel LaBell */
/* /home/dlabell/src/C/getch.c */
/*  file-created: Tue Feb  3 02:00:37 1998 */
#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* getch and getche returns -1, or the char
 * getchp/getchpe returns -1 if no char is pending, -2 on error, or the char
 */

int getch(void)
{
  struct termios term_settings,term_settings_saved;
  int x;
  if ( tcgetattr(STDIN_FILENO,&term_settings))
    return -1;
  term_settings_saved=term_settings;
  term_settings.c_lflag &= ~ICANON ;
  term_settings.c_lflag &= ~ECHO ;
  term_settings.c_cc[VMIN]=1 ;
  term_settings.c_cc[VTIME]=0;
  if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
    return -1;
  x=getchar();
  tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  return x;

Quote:}

int getche(void)
{
  struct termios term_settings,term_settings_saved;
  int x;
  if ( tcgetattr(STDIN_FILENO,&term_settings))
    return -1;
  term_settings_saved=term_settings;
  term_settings.c_lflag &= ~ICANON ;
  term_settings.c_lflag |= ECHO ;
  term_settings.c_cc[VMIN]=1 ;
  term_settings.c_cc[VTIME]=0;
  if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
    return -1;
  x=getchar();
  tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  return x;

Quote:}

/* getch pending or getch predicate if you like */
/* this is something like '?key' in forth, or was it 'key?' ? */

int getchp(void)
{
  struct termios term_settings,term_settings_saved;
  char c;
  int x;
  if ( tcgetattr(STDIN_FILENO,&term_settings))
    return -2;
  term_settings_saved=term_settings;
  term_settings.c_lflag &= ~ICANON ;
  term_settings.c_lflag &= ~ECHO ;
  term_settings.c_cc[VMIN]=0;
  term_settings.c_cc[VTIME]=0;
  if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
    return -2;
  switch(read(STDIN_FILENO,&c,1))
    {
    case 0: x=-1; break;
    case -1: x=-2; break;
    default:     x=c;
    }
  tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  return x;

Quote:}

int getchpe(void)
{
  struct termios term_settings,term_settings_saved;
  char c;
  int x;
  if ( tcgetattr(STDIN_FILENO,&term_settings))
    return -2;
  term_settings_saved=term_settings;
  term_settings.c_lflag &= ~ICANON ;
  term_settings.c_lflag |= ECHO ;
  term_settings.c_cc[VMIN]=0;
  term_settings.c_cc[VTIME]=0;
  if (tcsetattr (STDIN_FILENO, TCSANOW, &term_settings) < 0 )
    return -2;
  switch(read(STDIN_FILENO,&c,1))
    {
    case 0: x=-1; break;
    case -1: x=-2; break;
    default:     x=c;
    }
  tcsetattr (STDIN_FILENO, TCSANOW, &term_settings_saved);
  return x;

Quote:}

/* test prog */
#include <stdlib.h>

int pause_message(const char * message)
{
  if ( message )
    puts(message);
  return getch();

Quote:}

int main(int arc, char * argv[])
{    
  int x;
  char c;
  if (!(isatty(STDIN_FILENO)))
    {
      fprintf(stderr, "Not a terminal.\n");
      exit(2);
    }
  system ("ls");
  pause_message( "\npress almost any key to continue");
  puts("enter an integer number ");
  scanf( "%i", &x);
  printf( "5 + your number = %i\n", (5+x));
  pause_message( "\npress almost any key to continue");
  puts("Enter a single char: ");
  c=getch();
  printf("\n you typed %c, which is %hi in ascii\n", c, (short)c);
  pause_message( "\npress almost any key to continue");
  while ( getchp() < 0 )
    printf("After thats comes %hi\n", (short)++c);
  return  pause_message( "\npress almost any key to quit");

Quote:}

---------------------------------------------------------------
Don't know how portable this might be.
I have, however, just tested it on linux, and  sunos5.5.1
And it seems ok, except that getchp() is /not/ perfect.
(Probably should clear type-ahead buffer.. and if the user presses a key
while the program is actually printing.. echo will be on.. maybe more

Oh yeah, and its not terribly efficient to set/and restore terminal modes
non-stop.. However it does hide the complexity, which is probably what
you want.. And the time spent saving/restoring is probably small compared
to the time it takes a user to press a key.
--

        Dan

 
 
 

I need a getch() - Any help?

Post by Jeff S. Dickso » Sun, 08 Feb 1998 04:00:00



> ) Frankly, I am not familiar with UNIX, and I am definately slow at getting
> ) around the text-based OS.
> )
> ) Thanks for any help.
> )

Well this guy doesn't strike me as being too speedy either. See the
FAQ. This question has been brought up zillions of times.

                http://www.ibrado.com/sock-faq/

Jeff

 
 
 

I need a getch() - Any help?

Post by Jeff S. Dickso » Tue, 10 Feb 1998 04:00:00




> > ) Frankly, I am not familiar with UNIX, and I am definately slow at getting
> > ) around the text-based OS.
> > )
> > ) Thanks for any help.
> > )

> Well this guy doesn't strike me as being too speedy either. See the
> FAQ. This question has been brought up zillions of times.

>                 http://www.ibrado.com/sock-faq/

> Jeff


Sorry. Wrong URL. Right is: http://www.whitefang.com/unix/

Jeff

 
 
 

1. something like getch needed

something like getch needed, is there any kind of hack to get the same
effect as getch?

Heres a hack for  kbhit()

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int kbhit(void)
{
    fd_set rfds;
    struct timeval tv;

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    if(select(1, &rfds, NULL, NULL, &tv))
    {
        return(1);
    }

    return(0);

Thank you for the help,

John DeBoard

2. SCO Office Portfolio on OSR5.0.4

3. Need getch() or similar function

4. Netscape startup dial-out

5. Help getch problems

6. X related questions

7. curses getch help

8. Yet another FreeBSD<-->Linux PLIP problem

9. HELP ON GETCH

10. Help with getch()

11. Need help getting connected, bootp help needed

12. How do I do getch() or getchar() without pressing Enter

13. Writing a getch() function