tags:

views:

36

answers:

1

Basically, I have key detection for my console application, for some reason it's not detecting function keys.

Here is my code, I'm using GNU compiler on linux. Any help or ideas would be greatly appreciated.

        refresh();
        key = getch();
        switch(key) {
            case KEY_HOME:      key = HOME;   break;
            case KEY_END:       key = END;    break;
            case KEY_UP:        key = UP;     break;
            case KEY_DOWN:      key = DOWN;   break;
            case KEY_LEFT:      key = LEFT;   break;
            case KEY_RIGHT:     key = RIGHT;  break;
            case KEY_NPAGE:     key = PGDN;   break;
            case KEY_PPAGE:     key = PGUP;   break;
            case KEY_DC:        key = DEL;    break;
            case KEY_IC:        key = INSERT; break;
            case KEY_F(1):      key = F(1);   break;
            case KEY_F(2):      key = F(2);   break;
            case KEY_F(3):      key = F(3);   break;
            case KEY_F(4):      key = F(4);   break;
            case KEY_F(5):      key = F(5);   break;
            case KEY_F(6):      key = F(6);   break;
            case KEY_F(7):      key = F(7);   break;
            case KEY_F(8):      key = F(8);   break;
            case KEY_F(9):      key = F(9);   break;
            case KEY_F(10):     key = F(10);  break;
            case KEY_F(11):     key = F(11);  break;
            case KEY_F(12):     key = F(12);  break;
            case KEY_ENTER:     key = ENTER;  break;
            case KEY_BACKSPACE: key = BACKSPACE; break;
            default:
                //key = F(2); //For any function keypress, it jumps to default
                if (NON_ASCII(key) != 0)
                    key = UNKNOWN;
        }    
+2  A: 

I'm not a curses expert, but a bit of reading man pages netted me this program:

#include <curses.h>

int main()
{
    int key;

    initscr(); cbreak(); noecho();

    while (1)
    {

        key = getch();
        printw ("%u\n", key);
    }

    return 0;
}

When I press an F key, I get a 3-character sequence: 27, 79, (80 + N-1) where N is the number of the F key. I think your switch will have to recognize that the key is an escape sequence and handle it specially.

Edit: That pattern holds only for F1-F4. F5 changes it up. You'll probably want to incorporate the F(n) macros from curses.

Nathon