LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2005, 02:06 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
simple question


Is there a C library call to tell if any key has been pressed, eg if I have a while loop like:
Code:
while (???)
{
     puts("looping");
     sleep(1);
}
,so when I hit a key it will jump out of the loop
 
Old 04-03-2005, 04:15 PM   #2
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 58
hi there
maybe u would like to use some signals

there u can specify what action to perform on recieving a signal (like ctrl+d)

regards
 
Old 04-03-2005, 04:19 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
There is, but you gotta use some of termios' functions (man termios).
This is a quick example using termios:

Code:
#include <termios.h>
#include <unistd.h>

int main() {
  struct termios t;
  tcgetattr(0,&t);       /* gets params for stdin */
  t.c_lflag &= ~ICANON;  /* no longer line buffered */
  tcsetattr(0,0,&t);     /* make our changes */
  getchar();             /* will return immediately after key press */
  t.c_lflag |= ICANON;   /* back to line buffering */
  tcsetattr(0,0,&t);     /* works like before now */
}
 
Old 04-03-2005, 04:34 PM   #4
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 58
hi thwre

so we have getchar() working in linux with termios.h
i have used that with conio.h in DOS

does getch() also work??

regards
 
Old 04-03-2005, 04:37 PM   #5
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 58
UPDATE on the above post!!

getch and getchar both work using termios.h

thats a good thing for me
i thought we cannot use that in unix/linux

regards
 
Old 04-03-2005, 05:01 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
It works for anything that reads from stdin

Normally stdin is line buffered, which means anything that reads from it won't actually get the data until someone hits Enter. So, if you wanted a function that just waits for any key to be hit, it won't work. That's where termios comes in handy, it let's you turn off the default line buffering, so functions like getch(ar) will work as soon as any key is pressed (and not when someone hits enter).

Last edited by 95se; 04-03-2005 at 05:04 PM.
 
Old 04-04-2005, 03:38 AM   #7
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
thanks for everything
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 03:03 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 10:03 PM
simple question vadertech Linux - Newbie 14 01-26-2004 08:41 PM
Simple(hopefully) question magyartoth Linux - Networking 13 04-04-2002 03:24 PM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration