Home > Archive > Unix Programming > June 2006 > Stop Endless Loop with Keyboard Events in Linux
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Stop Endless Loop with Keyboard Events in Linux
|
|
| eventuallyanyway@gmail.com 2006-06-17, 8:21 am |
| I am fairly new to C++ and I'm reading about 8 books on the topic all
at the same time. I've read these books, done their examples, but I
want to start playing with concepts that are inside my head. I've used
Google for information on the problem, and searched the groups, and
used IRC channels...
The problem: It's just a concept right now, but here's what I've got:
#include <iostream>
int main() {
int myNumber = 0;
for ( ; ; ) {
std::cout << "myNumber is " << myNumber << "\n";
myNumber++;
}
}
Now, obviously, this is just an endless loop, incrementing a number and
then printing it out. Simple idea, right? Now, the trick is that I
would like to be able to escape the loop when someone hits the Escape
key on the keyboard. I'm using Linux, and someone in an IRC channel
suggested poll() or select() but I haven't been able to find any
documentation on it specifically (I get like, four pages into Google
before I find anything that SEEMS relevant) Someone said that the two
functions are found in glibc, but couldn't find anything to back that
up.
I'm also semi-familiar with multi-threading, although not exactly sure
how to do that in C++ Someone also suggested that, although there were
no details provided. I thought maybe some experienced programmers
could give me some ideas as to how to implement that, and also some
design tips for the future when I actually use this is code. I've read
a few design books, but I don't think they'll mean anything to me
without some more C++ experience...
Your help is appreciated.
| |
| Pascal Bourguignon 2006-06-17, 8:21 am |
| eventuallyanyway@gmail.com writes:
> [...]
> Now, obviously, this is just an endless loop, incrementing a number and
> then printing it out. Simple idea, right? Now, the trick is that I
> would like to be able to escape the loop when someone hits the Escape
> key on the keyboard. I'm using Linux, and someone in an IRC channel
> suggested poll() or select() but I haven't been able to find any
> documentation on it specifically (I get like, four pages into Google
> before I find anything that SEEMS relevant) Someone said that the two
> functions are found in glibc, but couldn't find anything to back that up.
> [...]
You need to put the terminal in raw mode to avoid the terminal driver
to buffer the input lines, then you can read input byte by byte, or
use poll or select to be informed as soon as a new byte is available.
http://www.opengroup.org/onlinepubs...p11.html#tag_11
http://www.opengroup.org/onlinepubs...ml#tag_11_01_07
man 3 termios
--
__Pascal Bourguignon__ http://www.informatimago.com/
ADVISORY: There is an extremely small but nonzero chance that,
through a process known as "tunneling," this product may
spontaneously disappear from its present location and reappear at
any random place in the universe, including your neighbor's
domicile. The manufacturer will not be responsible for any damages
or inconveniences that may result.
| |
| davids@webmaster.com 2006-06-17, 8:21 am |
|
eventuallyanyway@gmail.com wrote:
> Now, obviously, this is just an endless loop, incrementing a number and
> then printing it out. Simple idea, right? Now, the trick is that I
> would like to be able to escape the loop when someone hits the Escape
> key on the keyboard. I'm using Linux, and someone in an IRC channel
> suggested poll() or select() but I haven't been able to find any
> documentation on it specifically (I get like, four pages into Google
> before I find anything that SEEMS relevant) Someone said that the two
> functions are found in glibc, but couldn't find anything to back that
> up.
Google for 'unix' and 'kbhit'. This is a FAQ, and the most common way
it's asked is "what's the equivalent of 'kbhit' on <some UNIX
variant>?" The 'kbhit' function was an old DOS function that returned a
'yes a key was hit' or 'no a key was not hit' value. It's not so simple
on UNIX, and if you see a port of 'kbhit', you'll probably laugh.
DS
|
|
|
|
|