Trouble registering enter key.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LazarusHC
    New Member
    • Aug 2009
    • 3

    Trouble registering enter key.

    I'm currently trying to learn perl as well as perl curses. I found this script, and I understand why it's supposed to work, but I don't understand why it doesn't.
    The idea is that pressing "enter" selects one of the menu entries, and if any other key is pressed, is prints "Character pressed is $c". The numbers work as expected, but if you press a letter, it acts like you've pressed enter.

    Code:
    #!/usr/bin/perl
    #
    # Copyright (C) 2003 by Virtusa Corporation
    # http://www.virtusa.com
    #
    # Anuradha Ratnaweera
    # http://www.linux.lk/~anuradha/
    #
    
    
    use Curses;
    
    $width = 30;
    $height = 10;
    $startx = 0;
    $starty = 0;
    
    @choices = (
        "Choice 1",
        "Choice 2",
        "Choice 3",
        "Choice 4",
        "Exit"
    );
    
    $n_choices = @choices;
    
    $highlight = 1;
    $choice = 0;
    
    initscr();
    clear();
    noecho();
    cbreak();
    $startx = ($COLS - $width) / 2;
    $starty = ($LINES - $height) / 2;
    
    $menu_win = newwin($height, $width, $starty, $startx);
    keypad(1);
    keypad($menu_win, 1);
    addstr(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
    refresh();
    print_menu($menu_win, $highlight);
    
    while (1) {
        $c = getch($menu_win);
        if ($c == KEY_UP) {
    	if ($highlight == 1) {
    	    $highlight = $n_choices;
    	}
    	else {
    	    $highlight--;
    	}
        }
        elsif ($c == KEY_DOWN) {
    	if ($highlight == $n_choices) {
    	    $highlight = 1;
    	}
    	else {
    	    $highlight++;
    	}
        }
        elsif ($c == '\n') {
    	$choice = $highlight;
        }
        else {
    	addstr($LINES - 2, 0, "Character pressed is $c");
    	refresh();
        }
        print_menu($menu_win, $highlight);
        last if ($choice);
    }
    
    addstr($LINES - 2, 0, "You chose choice $choice with choice string $choices[$choice-1]");
    getch();
    clrtoeol();
    refresh();
    endwin();
    
    sub print_menu {
        $menu_win = shift;
        $highlight = shift;
    
        $x = 2;
        $y = 2;
        box($menu_win, 0, 0);
        for ($i = 0; $i < $n_choices; $i++) {
    	if ($highlight == $i + 1) {
    	    attron($menu_win, A_REVERSE);
    	    addstr($menu_win, $y, $x, $choices[$i]);
    	    attroff($menu_win, A_REVERSE);
    	}
    	else {
    	    addstr($menu_win, $y, $x, $choices[$i]);
    	}
    	$y++;
        }
        refresh($menu_win);
    }
  • jlarson
    New Member
    • Sep 2009
    • 1

    #2
    I know it's a little late, but if anyone else has this problem I figured I might as well post.

    Line 63 is incorrect. This code was ported from C, where == works for everything, but perl likes to do things a little differently.

    This:
    Code:
    elsif ($c == '\n')
    should be:
    Code:
    elsif ($c eq "\n")
    Last edited by numberwhun; Sep 17 '09, 04:10 AM. Reason: Please use code tags!

    Comment

    Working...