Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Arrow Keys

by mbond (Beadle)
on Aug 28, 2001 at 04:44 UTC ( [id://108319]=perlquestion: print w/replies, xml ) Need Help??

mbond has asked for the wisdom of the Perl Monks concerning the following question:

My current task is to create a text based form that will be placed on older laptops in our building. The only requirement of the form that i am currently stuck on is that the user must be able to move between fields using the arrow keys.

My question is how do i catch the signal fromt he arrow keys (^[[A, etc ...)? Once i can catch it moving the cursor will be trival.

The OS is a X-less install of Linux.

Thanks in advance

mbond

Replies are listed 'Best First'.
Re: Arrow Keys
by blakem (Monsignor) on Aug 28, 2001 at 04:50 UTC
Re: Arrow Keys
by stefan k (Curate) on Aug 28, 2001 at 12:22 UTC
    A while back I rewrote a perl script which I took from a form where such a task was performed. I can't remember the details exactly but this seems to be the relevant code section:
    while ( $_=getc(STDIN) ) { last if /[q\r\003\032]/;# break on q,Return,Ctrl-C,Ctrl-Z $Index-- if /[8A]/; # up $Index++ if /[2B]/; # down $Index=0 if /[9H1]/; # home $Index=$LineCount if /[3F4]/; # end }
    It all takes place in a terminal which has been set to raw device (?):
    $oldtty = `stty --save`; system('stty raw -echo');

    Regards... Stefan

      After cobbling some stuff together, I got this working. Hope it helps someone else. Bill
      #!/usr/bin/perl -w
      
      use strict;
      use warnings;
      
      use IO::Handle;
      
      eval {
          my $stdin = new IO::Handle;
             $stdin->fdopen( fileno( STDIN ), "r" ) || die "Cannot open STDIN";
      
          system "stty -icanon -isig -echo min 1 time 0";
      
          while ( my $char = $stdin->getc() ) {
              print "Up\n"     if ( ord( $char ) == 65 );  # up
              print "Down\n"   if ( ord( $char ) == 66 );  # down
              print "Right\n"  if ( ord( $char ) == 67 );  # right arrow
              print "Left\n"   if ( ord( $char ) == 68 );  # left arrow
              print "Return\n" if ( ord( $char ) == 10 );  # return key
              print "Delete\n" if ( ord( $char ) == 127 ); # delete key
              print "Esc\n"    if ( ord( $char ) == 27 );  # escape key
              print "Tab\n"    if ( ord( $char ) == 9 );   # tab key
              next             if ( ord( $char ) == 32 );  # skip on space key
              last             if ( $char =~ /q/ );        # break on q
              last             if ( $char =~ /\003/ );     # break on Ctrl-C
              last             if ( $char =~ /\032/ );     # break on Ctrl-Z
              print "ord: " . ord($char) . "\n";
          }
          system "stty icanon echo isig";
          exit(0);
      };
      
      if ( $@ ) {
          print "$@\n";
          system "stty icanon echo isig";
          exit(1);
      }
        heh... I posted the last bit of code, but wanted to mention that I have not found a good source for the escape codes (\###). Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://108319]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found