• C

Interactive Keyboard Input

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.
grobinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rbrCommented:
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

0
Ready4DisCommented:
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.
0
rbrCommented:
I have sent the program to you. So if you have problems pls ask, but don't reject my answer without any comment.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

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

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

0
rbrCommented:
Did you link with -lcurses?
You need the library libcurses.a.
0
grobinAuthor Commented:
Okay, I linked it with -lcurses and it compiled but when I run it I get a segmentation fault.
0
jwilcoxCommented:
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)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.