Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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.
0
grobin
Asked:
grobin
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now