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) Thanks. |
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 |
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.
|
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 |
Thanks for the info. I cannot check right away, but I'll let you know as soon as I try it. Thanks again.
|
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. |
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". |
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 |
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.
|
can't you use signal to do it?
|
Quote:
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. |
Quote:
:-) |
Quote:
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 |
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 |
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 12:54 PM. |