LinuxQuestions.org
Help answer threads with 0 replies.
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 05-03-2010, 06:05 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,634
Blog Entries: 29

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Differences between ncurses library and termios struct w.r.t Keyboard reading


I want to read a pressed key or a combination of pressed keys from the keyboard and perform some action afterwords.

e.g.
Ctrl-Alt-F1


Out of ncurses lib. and the termios struct which can be used best for the above purpose and why ?

I tried to search on Google, the differences between these two but couldn't get much !

Kindly guide !

Last edited by TheIndependentAquarius; 05-03-2010 at 06:23 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-04-2010, 01:54 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"curses" is relatively high-level; "termio" is extremely low-level.

If you're talking about one low-level key input in one specific part of your program, and termio gives you what you want - then go for it.

If you want generalized, cross-platform, "text mode screen handling" capabilities, then ncurses is generally an excellent choice.

If you just want to read the raw, binary input of a specific key or key combination and you can get away with standard "printf" and "scanf" type input for the rest of your program, then "termio" might be the better choice.

IMHO .. PSM
 
2 members found this post helpful.
Old 05-04-2010, 01:54 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
I'm sure others have different opinions, but here's my take. If you want much of what ncurses offers (character-oriented windows, subwindows, menus, and the like), use ncurses. Otherwise, stick with struct termios, so you don't add needless complexity to your program's functionality.
 
1 members found this post helpful.
Old 05-04-2010, 02:27 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,634
Blog Entries: 29

Original Poster
Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
paulsm4
and
wje_lq

Many thanks to both of you for these enlightening replies !

I used termios for the above task as i too soon realized that ncurses would be too heavy for all this !
I have shown my C code in the next post.

Quote:
Originally Posted by paulsm4
IMHO .. PSM
I have seen you posting like this before too !
Would you be kind enough to tell me what does this mean so that i can respond accordingly ?

Last edited by TheIndependentAquarius; 05-04-2010 at 02:36 AM.
 
Old 05-04-2010, 02:32 AM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,634
Blog Entries: 29

Original Poster
Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Compiled C Code for reading key-strokes from keyboard !

Important Link:
http://www.win.tue.nl/~aeb/linux/kbd...odes.html#toc1

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

int getch()
{
  struct termios oldt, newt;
  int ch;

  tcgetattr( STDIN_FILENO, &oldt );

  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );

  tcsetattr( STDIN_FILENO, TCSANOW, &newt );

  ch = getchar();
  
  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  return ch;
}


int main ()
{
  char ch;
  while (1)
    {  
      ch = getch();
  
      if (ch == 125)
	printf ("\nYou pressed SHIFT+]");
      
      else if (ch == 27)
	{
	  ch = getch();
	  if (ch == 91)
	    {
	      ch = getch();
	      if (ch == 65)
		printf("\nYou pressed UP arrow");
	      else if (ch == 66)
		printf("\nYou pressed DOWN arrow");
	      else if (ch == 67)
		printf("\nYou pressed RIGHT arrow");
	      else if (ch == 68)
		printf("\nYou pressed LEFT arrow");
	    }
	}
    }
}

Last edited by TheIndependentAquarius; 05-04-2010 at 02:35 AM.
 
1 members found this post helpful.
Old 05-04-2010, 11:33 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
Would you be kind enough to tell me what does this mean
"IMHO" is an acronym that means "In My Humble Opinion". I'm trying to say "You're welcome to disagree with me ... but I strongly believe what I said to be true"

"PSM" is just my initials ("Paul Michael John Santa Maria" )

Glad we were able to help!
 
1 members found this post helpful.
Old 05-05-2010, 12:27 AM   #7
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,634
Blog Entries: 29

Original Poster
Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Thanks for the explanation Paul,

You know I looked for the slang PSM on Google and found "Please See Me". Then I wondered that how do you want me to respond to that !

Last edited by TheIndependentAquarius; 05-05-2010 at 12:51 AM.
 
Old 11-22-2014, 02:19 PM   #8
luccasrodge
LQ Newbie
 
Registered: Nov 2014
Posts: 1

Rep: Reputation: Disabled
Talking ...

Thank so much TheIndependentAquarius ._____.

Last edited by luccasrodge; 11-22-2014 at 02:21 PM.
 
1 members found this post helpful.
Old 11-22-2014, 02:44 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,949

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
A note: termios(2) won't read anything, read(2) will. Decoding/interpreting escape sequences is no way trivial; for example midnight commander does some interesting dark magic so that Shift+F5 would work as F15 (instead of F17 or whatnot.)
 
1 members found this post helpful.
  


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
differences between "struct sock" and "struct socket" ahm_irf Programming 4 03-30-2009 11:29 AM
gcc differences with C-struct initialization ta0kira Programming 2 08-09-2008 01:55 PM
reading struct elf_prstatus for process info in linux? sanjaykhuntia Linux - Enterprise 0 10-26-2007 04:56 AM
struct reading and writing to file ilnli Programming 8 08-20-2007 10:54 AM
how to use ncurses library skie_knite007 Programming 1 03-01-2005 11:51 AM


All times are GMT -5. The time now is 06:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration