473,479 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble registering enter key.

3 New Member
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
1 1826
jlarson
1 New Member
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

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

Similar topics

1
2315
by: windandwaves | last post by:
Hi Folk, Below is a message that I posted on alt.html I now have some more feedback from people who experienced an error with safari and they tell me that the PHPsessID changes while running a...
2
1976
by: glenn | last post by:
I have written a COM Server in C# and its working perfectly. I am not ready to deploy to client computers to begin the real testing and am now wondering how to perform. The part that confuses me...
13
3023
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
8
2074
by: teddarr | last post by:
I'm having trouble getting a mathmatical formula to work in my code. I am supposed to write a program in java that calculates the ending balance each month. The user is supposed to input the...
1
2171
by: shivkumar2004 | last post by:
Hi!, I am developing a chat system using vb.net in vs 2005. I am getting the following error while registering the events. error details: System.InvalidOperationException was unhandled...
0
1196
by: shivkumar2004 | last post by:
Hi, I m getting the following error while registering the events on client appl. error: "An error occurred creating the form. See Exception.InnerException for details. The error is: Exception...
4
1987
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
0
1144
by: jthep | last post by:
Hi, I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in...
2
2205
by: linuxfedora | last post by:
I have written a C# program which has an activeX control (abc.ocx) in Visual Studio 2005, and used the setup and deployment project to create an setup.exe and setup.msi that act as an installer. ...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.