LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 11-26-2005, 08:07 PM   #1
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 175
Blog Entries: 1

Rep: Reputation: 19
How to get keystrokes w/o ncurses


I've written a few C programs for interactive or conversational use in a terminal, or even outside of Xwindows in a command line session. Currently, i'm using ncurses in raw mode with echo turned off, so i can get immediate response with no buffering and no translation by the ncurses routines.

But i'd really like to get the keystrokes without linking ncurses in, for various reasons. There doesn't seem to be a system call to do this. Am i missing something that's so easy i can't see it?

 
Old 11-26-2005, 08:16 PM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
You must setup the terminal in raw mode, then you reset it to the previous state when you're done. There's an example here:
http://yendor.com/programming/unix/a...vr4/ttymodes.c
 
Old 11-26-2005, 08:45 PM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 175
Blog Entries: 1

Original Poster
Rep: Reputation: 19
Thanks. I will study this sample code.
I think it's exactly what i was looking for, but i may come back with more questions.
.
.
.

.
.
.
I spoke too soon. This code is helpful, but i still don't see how i get a keystroke. I'm looking for the equivalent of a ncurses (or ms c) getch() function, or a bios int 10 call. So i repeat: is this so simple that i can't see it? What am i missing?

Last edited by dogpatch; 11-27-2005 at 12:54 AM.
 
Old 11-27-2005, 05:36 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
You have to setup the terminal in raw mode and then getchar() may do. Also, use setbuf(stdin, NULL) to turn off standard buffering.

Summary:
Setting the terminal in raw mode turns off the buffering done by the line terminal driver. Then you need to turn off the buffering done by the standard library itself.
 
1 members found this post helpful.
Old 11-27-2005, 09:05 PM   #5
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 175
Blog Entries: 1

Original Poster
Rep: Reputation: 19
Yes, now getchar() works, after setting the terminal into raw mode. For that matter, fread to stdin also works. And i seem to get unbuffered input even w/o the setbuf call. But i will keep that in mind as well.

Now, is there a flag in the termio structure that will tell me whether there is indeed any keystrokes waiting in the queue?
 
Old 11-27-2005, 10:14 PM   #6
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
I'm not aware of a way with the termios API. You may try with select() or asynchronous I/O... select() will work easily. See "man select_tut" and termios(3). The FreeBSD manpage is good too:
http://www.freebsd.org/cgi/man.cgi?q...SE&format=html
 
Old 11-27-2005, 11:03 PM   #7
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 175
Blog Entries: 1

Original Poster
Rep: Reputation: 19
Thanks. The link you provided may have what i'm looking for. More studying
.
.
.
OK, i'm back. Turns out to be quite easy. I just changed one line in the tty_raw() routine:
Code:
	buf.c_cc[VMIN] = 1;	/* Case B: 1 byte at a time, no timer */
  	buf.c_cc[VTIME] = 0;*/
becomes
Code:
	buf.c_cc[VMIN] = 0;	/* Case D: No minimum, no timer */
	buf.c_cc[VTIME] = 0;
Now, if there are no keystrokes in the queue, the getchar() returns -1, instead of waiting for a keystroke.

Thank you for your help. I'm off & running with this now.

Last edited by dogpatch; 11-28-2005 at 01:15 PM.
 
  


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
Sending Keystrokes lm317t Linux - Software 0 10-10-2005 05:02 PM
kde captures keystrokes vose Linux - Software 3 12-17-2004 08:14 AM
Trapping keystrokes in C k4zau Programming 3 03-09-2003 02:13 PM
Second Terminal Keystrokes Guinney Linux - Newbie 2 11-29-2002 09:18 PM
ncurses-5.2-28 conflicts with file from package ncurses-5.2-12 tubby Linux - Software 4 06-16-2002 01:00 AM


All times are GMT -5. The time now is 05:16 AM.

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