PDA

View Full Version : Function keys and strange results



divineleft
11-10-2006, 07:58 PM
Hello.

I'm writing a small game for fun and I'm having a problem with the function keys, specifically F1-F4.



#include <curses.h>

void input()
{
int choice;
choice = getch();
printw("%c\n", choice);
refresh();
}
When I press any other function key, the character returns as "^Q" or "^R", but for some reason those 4 keys (F1-F4) return things like "^[[11~". Even when I do something like:



if(choice == KEY_F(1)) // or F2,F3,F3
{
...
}

it doesn't work. But, If I use any of the other function keys, it works. Is this a hardware problem on my side, or am I just doing something wrong? It's not just for ncurses either, it happens regardless.

I'm on linux, if that's of any help.

Salem
11-11-2006, 02:24 AM
Can you post a short and complete ncurses program which prints your function keys?

Your F1 to F4 keys are generating ANSI Escape sequences, which is something they can be programmed to do in various kinds of terminal emulation modes. How you change that depends on what you're using.

divineleft
11-11-2006, 06:33 AM
Sure.



#include <curses.h>
#include <iostream>


void menu();

int main()
{
initscr();
keypad(stdscr, TRUE);
noecho();
scrollok(stdscr, TRUE);

menu();

endwin();
return 0;
}

void menu()
{
int choice;
while (choice = getch())
{
printw("%c\n", choice);
refresh();
}
}

jim mcnamara
11-12-2006, 06:39 AM
ncurses returns an integer - some keys like F1 send 265, if I remember, by default in ncurses. Which is beyond the limits for unsigned char. getch() returns an integer.