Solved

Interactive Keyboard Input

Posted on 1998-01-14
7
283 Views
Last Modified: 2010-04-02
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
Comment
Question by:grobin
7 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1256995
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
 
LVL 2

Expert Comment

by:Ready4Dis
ID: 1256996
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
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
ID: 1256997
I have sent the program to you. So if you have problems pls ask, but don't reject my answer without any comment.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:grobin
ID: 1256998
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
 
LVL 10

Expert Comment

by:rbr
ID: 1256999
Did you link with -lcurses?
You need the library libcurses.a.
0
 

Author Comment

by:grobin
ID: 1257000
Okay, I linked it with -lcurses and it compiled but when I run it I get a segmentation fault.
0
 
LVL 1

Expert Comment

by:jwilcox
ID: 1257001
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now