2012-04-23 80 views
0

是否有可能以類似的方式得到光標位置的保持與Term::TermKeyTerm::ReadKey可以做到這一點:是否可以用Term :: TermKey捕捉光標位置?

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

ReadMode 4; 

system('clear') == 0 or die $?; 
print "Hello world\n" x 4; 
print "go to column 21 -> |"; 

print "\e[6n"; 
my ($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; 
       } 
      } 
     } 
    } 
} 

回答

2

還沒有,但我正在爲它的計劃。它可能會被報告爲一個新的事件類型,看起來像:

use Term::TermKey; 

my $tk = Term::TermKey->new; 

syswrite STDOUT, "\e[6n"; 

while($tk->waitkey(my $key)) { 
    if($key->type_is_position) { 
    printf "The cursor is at %d, %d\n", $key->line, $key->col; 
    } 
} 

需要在底層C庫一些額外的支持,首先,包括掛鉤其他CSI序列的能力。一旦出現這種情況,未來應該更容易支持更多,例如通過CSI提供的許多其他狀態報告。


編輯2012/04/26:我現在已經發布libtermkey 0.15和0.14 Term::TermKey如上所述,它有這個API。

+0

我無法嘗試它,因爲我不知道如何安裝libtermkey 0.15(我希望有一個Alien :: libtermkey更新:))。 – 2012-04-30 06:28:08

+0

你知道你可以從原始上游tarball安裝...;) 此外現在還有一個Alien :: libtermkey更新。 – LeoNerd 2012-05-01 18:18:52

+0

當我用'type_is_position'替換'is_position'時,它起作用。 – 2012-05-02 07:48:53

相關問題