Interactive Keyboard Input

grobin
grobin used Ask the Experts™
on
Well, I'm not sure if _Interactive Keyboard Input_ is the best title for this question (I guess all keyboard input is interactive) but I can't think of a shorter description.

Anyway, I'm a fairly new C programmer and consequentially have a beginner level book (_Teach Yourself C in 21 Days_) that has left out some, I think, important stuff. Most notably is the ability to read the last character in the keyboard buffer and not require Enter to be pressed (something similar to a command in QBasic called inkey$). All the functions the book talks about like gets() and getschar() stop program execution and wait for Enter to be pressed before continuing. I asked somebody about it and they mentioned a command they use in C++ that sounds like it has something to do with the BIOS. Can somebody please tell me how to do it in C and give me some sample code? Please note that this is for UNIX, so if it involves an operating system call it probably wont work.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
rbr

Commented:
I can give you an example program for input routines for many OS
(Dos, Os/2, Unix, VMS, ...). Pls send an email to rbr@physik.kfunigraz.ac.at

I know it, but someone gave a little answer that doesn't answer anything.  Well, you could do two of one things, keyboard handler, or just use the C command.  Reject his answer, or E-Mail him to give others a chance.
Commented:
I have sent the program to you. So if you have problems pls ask, but don't reject my answer without any comment.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I replied to your email with a question a long time ago but didn't get a reply. Possibly you never received it and it was a little presumptuous of me to reject it like that so I'll ask again.

The code you gave me won't compile on my system but I was able to learn about the getch() command from it. I read up on getch() from the man pages and tried to write the following program to test it

#include <stdio.h>
#include <ncurses.h>

main ()
{
   int ch_buffer;
   
   while(1)
   {  ch_buffer = getch();
      printf ("%d\n",ch_buffer);
   }
}

When I try to compile this I get the errors

Undefined symbol '_stdscr' referenced from text segment
Undefined symbol '_wgetch' referenced from text segment

Do you know how to make this work?

Thanks and sorry about that.

rbr

Commented:
Did you link with -lcurses?
You need the library libcurses.a.

Author

Commented:
Okay, I linked it with -lcurses and it compiled but when I run it I get a segmentation fault.

Commented:
getch seems to be a bit unstable(atleast on Linux 2.x), also unless you are interested in ASCII values I would switch your int ch_buff to char ch_buff, C will convert it from int to char for you, it is int purely for the sake of EOF.

 This code should work for you:

#include <stdio.h>
#include <termios.h>

int main()
{
 char ch_buff;
 struct termios inital_settings, new_settings;

  tcgetattr(fileno(stdin), &inital_settings); /* Get the                                      inital terminal settings */
  new_settings = initalsettings;
  new_settings.c_lflag &= ~ICANON; /* Don't wait for newline */
  new_settings.c_cc[VMIN] = 1; /* Wait for one char to be read */
  new_settings.c_cc[VTIME] = 0; /* No time constratins */
                         /* Put our new settings into effect
                            if there is an error, quit */
  if(tcsetattr(fileno(stdin), TCSANOW, &new_settings) != 0)
  {
   fprintf(stderr, "Could not set attributes\n");
   exit(1);
  }  
  while(1)
  {
   ch_buff = getchar();
   printf("\nInput: %c\n", ch_buff);
  }
             /* Return the settings to normal */
  tcsetattr(fileno(stdin), TCSANOW, &inital_settings);
  exit(0);
}  

keeping ch_buff as int will still work, but will give ASCII values rather than charactrs in return(ohh and the %c needs to become %d again)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial