Results 1 to 5 of 5
Using system() or terminal I can set the input mode to raw instead of cooked with "stty raw -echo" which lets me know if a key was pressed using getchar(), ...
- 01-25-2012 #1Just Joined!
- Join Date
- Jan 2012
- Posts
- 1
find out if NO keys are being pressed
Using system() or terminal I can set the input mode to raw instead of cooked with "stty raw -echo" which lets me know if a key was pressed using getchar(), but I am still confused about how to find out if no key is currently being pressed. Any suggestions?
Preferably in C/C++, but bash would be fine too.
Thanks!
- 01-26-2012 #2Just Joined!
- Join Date
- Jan 2011
- Location
- Fairfax, Virginia, USA
- Posts
- 94
Hi NotBash,
I'm confused by your post. Typically the way people determine if input is available is to select(2) or poll(2) in file descriptor 0. File descriptor 0 is #define'd in one of the standard headers as STDIN_FILENO. Cooked or raw terminal modes don't seem to apply here as far as I can see.
- 01-28-2012 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Well, if there is NO event, there is NO event... IE, no key press, then there is no key press... If you want to know if there is no key press for some period, then select() with a timeout will do the job nicely. If you want to know that there is no key press currently, then you can assume that if you are not in an event handler, there is no key press active...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 01-29-2012 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
In bash, there is a time-out feature for the read command. That suggests that within a given time frame no key would have been pressed. See man bash for details:
Best wishes ... cheers, drlCode:read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] One line is read from the standard input ... excerpt from man bashWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 01-29-2012 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Thanks drl. I wasn't aware of the read -t timeout option for bash scripts (never needed it before). Good call!
Only issue is that this is to read a line, until when an EOL or ^D are issued. I think that the poster is looking for ANY key press. At least it is useful to know.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


1Likes
Reply With Quote