Solved

Interactive Keyboard Input

Posted on 1998-01-14
7
287 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

830 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