By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

Trouble registering enter key.

P: 3
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) 2003 by Virtusa Corporation
  4. # http://www.virtusa.com
  5. #
  6. # Anuradha Ratnaweera
  7. # http://www.linux.lk/~anuradha/
  8. #
  9.  
  10.  
  11. use Curses;
  12.  
  13. $width = 30;
  14. $height = 10;
  15. $startx = 0;
  16. $starty = 0;
  17.  
  18. @choices = (
  19.     "Choice 1",
  20.     "Choice 2",
  21.     "Choice 3",
  22.     "Choice 4",
  23.     "Exit"
  24. );
  25.  
  26. $n_choices = @choices;
  27.  
  28. $highlight = 1;
  29. $choice = 0;
  30.  
  31. initscr();
  32. clear();
  33. noecho();
  34. cbreak();
  35. $startx = ($COLS - $width) / 2;
  36. $starty = ($LINES - $height) / 2;
  37.  
  38. $menu_win = newwin($height, $width, $starty, $startx);
  39. keypad(1);
  40. keypad($menu_win, 1);
  41. addstr(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
  42. refresh();
  43. print_menu($menu_win, $highlight);
  44.  
  45. while (1) {
  46.     $c = getch($menu_win);
  47.     if ($c == KEY_UP) {
  48.     if ($highlight == 1) {
  49.         $highlight = $n_choices;
  50.     }
  51.     else {
  52.         $highlight--;
  53.     }
  54.     }
  55.     elsif ($c == KEY_DOWN) {
  56.     if ($highlight == $n_choices) {
  57.         $highlight = 1;
  58.     }
  59.     else {
  60.         $highlight++;
  61.     }
  62.     }
  63.     elsif ($c == '\n') {
  64.     $choice = $highlight;
  65.     }
  66.     else {
  67.     addstr($LINES - 2, 0, "Character pressed is $c");
  68.     refresh();
  69.     }
  70.     print_menu($menu_win, $highlight);
  71.     last if ($choice);
  72. }
  73.  
  74. addstr($LINES - 2, 0, "You chose choice $choice with choice string $choices[$choice-1]");
  75. getch();
  76. clrtoeol();
  77. refresh();
  78. endwin();
  79.  
  80. sub print_menu {
  81.     $menu_win = shift;
  82.     $highlight = shift;
  83.  
  84.     $x = 2;
  85.     $y = 2;
  86.     box($menu_win, 0, 0);
  87.     for ($i = 0; $i < $n_choices; $i++) {
  88.     if ($highlight == $i + 1) {
  89.         attron($menu_win, A_REVERSE);
  90.         addstr($menu_win, $y, $x, $choices[$i]);
  91.         attroff($menu_win, A_REVERSE);
  92.     }
  93.     else {
  94.         addstr($menu_win, $y, $x, $choices[$i]);
  95.     }
  96.     $y++;
  97.     }
  98.     refresh($menu_win);
  99. }
  100.  
Aug 10 '09 #1
Share this Question
Share on Google+
1 Reply


P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. elsif ($c == '\n') 
  2.  
should be:
Expand|Select|Wrap|Line Numbers
  1. elsif ($c eq "\n")
  2.  
Sep 16 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.