LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Repeatedly checking for input (https://www.linuxquestions.org/questions/programming-9/repeatedly-checking-for-input-182726/)

kamransoomro84 05-17-2004 11:25 PM

Repeatedly checking for input
 
Hi.


I wanted to know how I can repeatedly check whether a key has been pressed . For example, I want to run a while loop that is always true. Inside is an if statement that check whether a key has been pressed and exits if one has. Here's the equivalent DOS program.
Code:

while(true)
  if(kbhit() )
    break;

Can anybody help me. Also, do I use getchar() to retrieve the input from the stream. Like in DOS you use getch().

Thanks.

paulsm4 05-18-2004 12:04 AM

Try reading up on the "curses" library - it might be just what you're looking for.

Alternatively, you can put your /dev/tty console into "raw" (vs. "cooked") mode, then do binary reads (without having to wait for somebody to hit "<Enter>").

In general, you'd probably be better off blocking (until user input) instead of polling (which could consume 100% CPU while you're waiting for user input).

Hope that helps .. PSM

kamransoomro84 05-18-2004 06:16 PM

I don't want to use the curses library because it always messes up the console and is not recommended for C++. Also can you please explain what the diifference between raw and cooked mode is? I'm relatively new to Linux so I don't know a whole lot. Oh and what is polling and blocking. Thanks.

paulsm4 05-18-2004 08:59 PM

Hi -

I honestly believe that curses is the correct solution for your problem.

I believe that curses allows you to "save" the current screen contents - and certainly the current screen settings - before and after your console session.

I am absolutely certain there are no restrictions against using curses with C++. If you wish to code in C++ (and I encourage you to do so!), then it's strictly a matter of personal preference as to whether or not to use curses or some other solution.

As far as "raw" vs "cooked" - it applies to terminal I/O on any kind of Unix - not just Linux. It simply means that, my default, the system won't pass any keyboard input back to you until you type <Enter>. In other words, in "cooked" mode, terminal input is buffered and processed (backspace keys, tab expansion, etc etc). In "raw" mode, on the other hand, binary input from the terminal is read directly.

You should be able to learn more details by doing a "man ioctl".

Finally, as far as "polling" vs "blocking": it simply means "do I continuously keep checking until my input is available (polling), or does the system give me some way to go to sleep and be automatically notified when it's available (blocking I/O)?"

'Hope that helps - and please let us know how things worked out for you!

Sincerely .. PSM

kamransoomro84 05-18-2004 09:51 PM

Thanks for the info. I cannot check right away, but I'll let you know as soon as I try it. Thanks again.

kamransoomro84 05-19-2004 11:08 AM

Hi Again.

I checked the curses library out. It's cool. It has everything I needed. But there's just one problem. I can't use cout with it. It doesn't give any error or anything. It just doesn't do anything at all. Any remedy for that. I don't want to use printw that comes with ncurses. Thanks.

paulsm4 05-19-2004 01:06 PM

I don't know.

My guess is that you should probably be able to get "cout" working the way you want by playing with the I/O streams "ios" class.

SUGGESTION:
Create a new class for your screen output. Make sure you define a good interface for your class, one that effectively "hides" your class's implementation details.

Use "printw" for the time being.

Once you get your program working, try changing the class's implementation to use "cout" instead of "print2".

mvt 05-19-2004 02:00 PM

Consider trying the method shown in post #4 to the similar question that I asked. There is a whole sample C++ program there you can cut and paste to test it.

the thread title is: unbuffered stdin
in the programming section.

This link may work: http://www.linuxquestions.org/questi...hreadid=176039

It uses termios.

With various settings it can:
1. make getc() return immediately either with a chacacter if one is available or EOF
2. make getc() wait up to N tenths of a second for a character, returning it as soon as one is available, otherwise returning EOF.
3. make read(stdin) wait for a specific number of characters to be available
4. etc.

It doesn't require a new display surface as curses seems to.

Hope this helps.

Mark

kamransoomro84 05-19-2004 07:12 PM

Thanks Mark, that helped. I had no idea it was so difficult to do such a simple thing in Linux. In DOS it's really easy. Anyway thanx.

carol1980 05-19-2004 09:40 PM

can't you use signal to do it?

mvt 05-20-2004 10:02 AM

Quote:

I had no idea it was so difficult to do such a simple thing in Linux. In DOS it's really easy.
Don't take this as a general indication. It stems from the much greater flexibility in the Linux console window than in a DOS window. And it isn't really a hard fix, just a hard-to-find fix.

I've programmed professionally for nearly 30 years, and virtually everything (network communication, multiple threads, 3-D graphics using OpenGL, etc.) is so much easier on Linux than Windows that there is no contest.

Please, let's not start a war here... just my professional opinion. Other people do truly think that programming on Windows is better. Some people like brussel sprouts. Go figure.

Hko 05-20-2004 11:22 AM

Quote:

Some people like brussel sprouts.
Sorry, but I can not believe that.
:-)

kamransoomro84 05-20-2004 05:45 PM

Quote:

Don't take this as a general indication. It stems from the much greater flexibility in the Linux console window than in a DOS window.
Acrually mvt, I'd pretty much figured that out myself. Actually I said that thing intentionally. I've a friend who's a Linux nut. He's the one who introduced me to Linux in the first place. I wanted to see whether other people were as much dedicated to Linux as he is. Nothing personal. You know I said the exact same thing to him to see his reaction. And I could tell he didn't like it. Anyway thanx all.

Oh and you mentioned multi-threaded programming. Can you point me to some useful sites on that. I've been meaning to learn how to do that . Thanx

mvt 05-26-2004 01:43 PM

Hi kamransoomro84,

Just do a google search for pthreads. There's lots of info out there, and lots of examples. I don't have any sites that I use, because I just take one of my old working examples and modify it any time I need a new program.

I do have a text document with lots of hints and 'gotchas' that I've compiled on the subject over the years. If you E-mail me, I'll send it to you.
My e-mail address is my username on this forum @se.rr.com

Mark

itsme86 05-26-2004 03:12 PM

Code:

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

int kbhit(void)
{
  struct termios old, new;
  int ch;

  tcgetattr(0, &old);

  new = old;
  new.c_lflag &= ~(ICANON);
  new.c_lflag &= ~(ECHO);

  tcsetattr(0, TCSANOW, &new);

  ch = getchar();

  tcsetattr(0, TCSANOW, &old);

  if(ch == EOF)
    return 0;
  return ch;
}

int main(void)
{
  int ch;

  printf("Press a key!\n");

  while(1)
    if((ch = kbhit()))
      printf("You pressed %c!\n", ch);

  return 0;
}

It's in C instead of C++, but whatever :)


All times are GMT -5. The time now is 12:54 PM.