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 03-06-2003, 06:19 PM   #1
k4zau
Member
 
Registered: Feb 2003
Location: huntsville, al
Distribution: debian, opensolaris
Posts: 99

Rep: Reputation: 15
Trapping keystrokes in C


I am trying to build a program with C, which will run as a text-mode console application under Linux. I need some functions to respond on a keystroke, e.g. anytime the user presses 'h' a help screen pops up. I don't want the user to have to press [enter] after the keystroke and I also want to be able to capture the [enter] key by itself.

I've read that this can be done under curses, but I want this to remain a strictly text-mode app. I also read that there is a function getch() in <conio.h> that seems to be what I'm looking for; I put these into a sample program and the compiler (gcc) didn't recognize either one. Are those in C++ only? I'm pretty knowledgeable about Basic, but I know much less about C.

Thanks
 
Old 03-06-2003, 06:40 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
getch(0 does exist, but I don't remember the header it is in off-hand. conio.h sounds very familiar though.

Interesting, I just tried "man getch" and the system said it was in curses.h. It would probably do what you need.
 
Old 03-07-2003, 05:29 AM   #3
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Rep: Reputation: 15
Hi,
I don't think getch() will solve your problem if I've understood it, becasue you said the program should respond anytime "h" is pressed, but getch() works at a point, and is exactly an an unbuffered and not-echo getchar() only.
What you need is signal handling. So that the user can do whatever he is doing, and at any time he presses "Cntrl-h" or something you've defined, the execution passes to the handler and the popup help menu is visible.

adi
 
Old 03-09-2003, 02:13 PM   #4
k4zau
Member
 
Registered: Feb 2003
Location: huntsville, al
Distribution: debian, opensolaris
Posts: 99

Original Poster
Rep: Reputation: 15
Cool

Thanks for your suggestions.

I did another (better) search on the Net, and found some free sourcecode that emulates getch() under Unix/Linux. I'll probably try to use that.

Actually, the user will have a prompt in my program where commands will be entered; I just didn't want them to have to press Enter afterwards. Being as the prompt is always on the screen and there aren't going to be any other processes running, I'm pretty sure that getch() will work.
 
  


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
ksh Error trapping/logging Risc91 AIX 1 07-28-2005 11:31 PM
trapping packets mohit_garg Linux - Networking 6 07-10-2005 06:29 PM
Trapping NFS requests Jayendra Linux - Networking 0 03-01-2004 10:17 AM
Trapping an exit status chrisk5527 Linux - General 3 09-12-2003 10:02 PM
trapping signals at bootup Xpolloloco Slackware 4 08-23-2002 10:21 AM


All times are GMT -5. The time now is 08:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration