2012-02-13 49 views
0

如何捕捉變量中的行和列而不是打印出來(這裏是^[[12;1R)?終端控制轉義序列:如何通過「 e [6n」捕獲行和列?

#!/usr/bin/env perl 
use warnings; 
use 5.012; 

print "\n" x 10; 

say "\e[6n"; 
+0

我正要建議「使用Term :: TermKey」,但後來我意識到它實際上並不支持這一點。 Hrmmm .. 我會考慮這種功能請求,看看我能想出什麼 – LeoNerd 2012-04-22 22:47:34

回答

0

我發現這一點:

#!/usr/bin/env perl 
use warnings; 
use 5.012; 
use Term::ReadKey; 

ReadMode 4; 

system('clear') == 0 or die $?; 

print "\e[6n"; 
my ($x, $y) = getch(); 
say "Col: $x - Row: $y"; 


print "Hello world\n" x 6; 

print "\e[6n"; 
($x, $y) = getch(); 
say "Col: $x - Row: $y"; 


print "String\n" x 5; 
print "go to column 21 -> |"; 


print "\e[6n"; 
($x, $y) = getch(); 
say "Col: $x - Row: $y"; 

ReadMode 0; 

sub getch { 
    my $c = ReadKey 0; 
    if ($c eq "\e") { 
     my $c = ReadKey 0.10; 
     if ($c eq '[') { 
      my $c = ReadKey 0; 
      if ($c =~ /\A\d/) { 
       my $c1 = ReadKey 0; 
       if ($c1 ne '~') { 
        my $y = 0 + $c; 
        while (1) { 
         last if $c1 eq ';'; 
         $y = 10 * $y + $c1; 
         $c1 = ReadKey 0; 
        } 
        my $x = 0; 
        while (1) { 
         $c1 = ReadKey 0; 
         last if $c1 eq 'R'; 
         $x = 10 * $x + $c1; 
        } 
        return $x, $y; 
       } 
      } 
     } 
    } 
} 
0

抱歉過了好一會兒,我很久以前加入此功能,則忘了更新的問題;)

use Term::TermKey; 

my $termkey = Term::TermKey->new(\*STDIN); 

STDOUT->autoflush(1); 
print "\e[6n"; 

while($termkey->waitkey(my $key) == RES_KEY) { 
    if($key->type_is_position) { 
     printf "Position is %d, %d\n", $key->col, $key->line; 
     last; 
    } 
}