LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Differences between ncurses library and termios struct w.r.t Keyboard reading (https://www.linuxquestions.org/questions/programming-9/differences-between-ncurses-library-and-termios-struct-w-r-t-keyboard-reading-805611/)

Aquarius_Girl 05-03-2010 05:05 AM

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 !

paulsm4 05-04-2010 12:54 AM

"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

wje_lq 05-04-2010 12:54 AM

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.

Aquarius_Girl 05-04-2010 01:27 AM

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 ?

Aquarius_Girl 05-04-2010 01:32 AM

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");
            }
        }
    }
}


paulsm4 05-04-2010 10:33 PM

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!

Aquarius_Girl 05-04-2010 11:27 PM

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 ! :D

luccasrodge 11-22-2014 01:19 PM

...
 
Thank so much TheIndependentAquarius ._____.

NevemTeve 11-22-2014 01:44 PM

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.)


All times are GMT -5. The time now is 05:31 PM.