Detecting a key combination

Could anybody tell me how I can detect a particular key combination and perform a particular task on that event. e.g. if I press Ctrl + L on the shell then it clears the screen. Please tell me how it can be performed on my shell.
And how the Arrow Keys can be detected. I tried but pressing a key once is returning 3 values namely 26,27,(A,B,C,D) The last one differs only.

Please tell me how to call a function or to execute a line of code when a particular event occurs.

Thanks

Have a look at the curses library and it's getkey() function.

There are only so many of these that you can define, those things are defined for you in the man page for stty.

AS an example, suppose you want the backspace key to delete the previous character, then put a line like this in your .profile or .bashrc file in your home directory. The next time you login, it takes effect - or you can source the file to make it work right away

stty erase "^H"

The erase command tells stty to have your terminal erase a character - "^H" written just the way I have it. You need to define the keys the way you want them.

stty functions are part of the termios set of functions - try termios if you want to do this in C.

Could you please clarify my problem
I am making a program in C
Now I am getting the string from the user then How I can put it in that state that what user has hitted.
Any sample will me a great help

Thanks

How I can detect a particular Key Combination through programming in Unix.

to trap the signals there are methods but control+L is not detected as a signal so how can I detect that combination of Key Hit and how to detect the Arrow Key Hits as each key hit is itself returning three values
The first two values of all four keys is same only the last key value is different.

I tried to use the getkey function of curses and select and poll funtions but my problem is not solved please help...

Thanks

Let's try this again

  1. all keystrokes go thru the tty driver. Period. If your driver interprets them they are interpreted BEFORE you program ever sees them. termios() can remove/add settings.

  2. For 2 key ctrl sequences [A-Z] the terminal returns ASCII 1-26 (minus those interpreted by the driver as something else) ctrl-L is 12, ASCII form feed. There are other two-key sequences as well.

  3. If you want to be able to create special responses to other key sequences like the function keys or alt key combinations you use the curses or ncurses library.

In general, if you want to play with single key combinations like a GUI does, you have to set the terminal to non-canonical mode. I've posted the code to do that on the forums several times - you can search for it.

Also, I have posted the code to do that, but only one time. But I also wrote a FAQ article, scripts/programs/code posted to www.unix.com, which has a link to it. So if mobile01 had either looked at the faqs or used the search function, he would already have the answer.

In the following code I scan the Ctrl+L and it returned the value 12 but it needs a enter key. Is there any function in Unix (C) so that the typed char should not appear on the screen. Like one in windows available in conio.h. I din have the conio in Unix.Why?

main()
{
char i;
i=getc(stdin);

printf\("%d\\n",i\); 
if\(i==12\)
\{    system\("clear"\);
    printf\("\\b"\);
 \}   

}

Can you read the above posts? They tell you what to do and where to look - we are not writing the code for you. hint: non-canonical

Thanks to all for their kind help. I implemented it as following:

#include<termios.h>
#include<unistd.h>
main()
{
    char i;
    i=getch();
    
    printf("%d\n",i); 
    if(i==12)
    {    system("clear");
        printf("\b");
     }   
}

int getch(void) {
      int c=0;

      struct termios org_opts, new_opts;
      int res=0;
          //-----  store old settings -----------
      res=tcgetattr(STDIN_FILENO, &org_opts);

          //---- set new terminal parms --------
      memcpy(&new_opts, &org_opts, sizeof(new_opts));
      new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE |
ICRNL);
      tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
      c=getchar();
          //------  restore old settings ---------
      res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);

      return(c);
}

The only thing I have as a problem now is How to do both at the same time in my Program. As if I press Ctrl+L , Ctrl+P they perform their necessary action given to them while if the input is a command then it should appear on the screen as in the previous this is not the case. I tried to store that in a buffer but not successful.
If I use a buffer then how that will perform if it is followed by a Ctrl.

Mobile01,

Please use code tags, it makes it very hard to read when you do not use the tags and if you want people to help you you should make it as easy as you can for them.

Sorry, I will use tags from now onwards.

Thanks

Please help with the problem