Thread: Function keys and strange results

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    Function keys and strange results

    Hello.

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

    Code:
    #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:

    Code:
    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.
    Last edited by divineleft; 11-10-2006 at 08:26 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Sure.

    Code:
    #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();
        }
    }

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ToUnicodeEx and dead keys inside system wide hooks
    By RevengerPT in forum Windows Programming
    Replies: 1
    Last Post: 08-13-2009, 02:51 PM
  2. Strange Keyboard
    By MadCow257 in forum Tech Board
    Replies: 5
    Last Post: 05-12-2005, 10:54 PM
  3. Strange Direct Input Behaviour
    By Diamonds in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2003, 04:34 PM