2012-04-20 37 views
0

这适用于按键,但不适用于鼠标点击。我应该改变什么才能使其工作(Term::TermKey)?Term :: TermKey:捕捉mouseevents和按键的正确方法是什么?

#!/usr/bin/env perl 
use warnings; 
use 5.12.0; 
use utf8; 
use Term::TermKey qw(FLAG_UTF8); 
my $tk = Term::TermKey->new(\*STDIN); 
binmode STDOUT, ':encoding(utf-8)' if $tk->get_flags & FLAG_UTF8; 

while(1) { 
    my $key; 
    $tk->waitkey($key); 

    if ($key->type_is_mouse) { 
     my ($ev, $button, $line, $col) = $tk->interpret_mouse($key); 
     say "event : $ev"; 
     say "button: $button"; 
     say "line : $line"; 
     say "col : $col"; 
    } 
    else { 
     say "<", $tk->format_key($key, 0), ">"; 
    } 
} 
+0

binmode STDOUT,':encoding(utf-8)'if if $ tk-> get_flags&FLAG_UTF8; 你可能的意思是'编码(UTF-8)'这里 – LeoNerd 2012-04-21 22:50:29

+0

我认为它们是等价的。 – 2012-04-22 06:00:49

+0

首都'UTF-8'。 – LeoNerd 2012-04-22 22:48:13

回答

2

当我激活鼠标模式它的作品。

#!/usr/bin/env perl 
use warnings; 
use 5.12.0; 
use utf8; 
use Term::TermKey qw(FLAG_UTF8); 
my $tk = Term::TermKey->new(\*STDIN); 
binmode STDOUT, ':encoding(utf-8)' if $tk->get_flags & FLAG_UTF8; 

$|++; 

print "\e[?1003h"; 

say "Quit with \"q\""; 
while(1) { 
    my $key; 
    $tk->waitkey($key); 

    if ($key->type_is_mouse) { 
     my ($ev, $button, $line, $col) = $tk->interpret_mouse($key); 
     say "event : $ev"; 
     say "button: $button"; 
     say "line : $line"; 
     say "col : $col"; 
    } 
    else { 
     say "<", $tk->format_key($key, 0), ">"; 
     last if $tk->format_key($key, 0) eq 'q'; 
    } 
} 

print "\e[?1003l"; 
+0

就是这样。 'libtermkey'可以识别鼠标事件,如果它接收到它们,但由于它最初不会与终端通信,所以它不能启用鼠标模式。你的DEC模式1003就足够了。 – LeoNerd 2012-04-21 22:48:02