Reading mouse-event on konsole
#!/usr/bin/env perl
use warnings;
use 5.012;
binmode STDOUT, ':encoding(UTF-8)';
use Term::ReadKey;
sub getch {
my $tty_in = shift;
my $c = ReadKey 0, $tty_in;
if ( $c eq "\e" ) {
my $c = ReadKey 0.10, $tty_in;
if ( $c eq '[' ) {
my $c = ReadKey 0, $tty_in;
if ( $c eq 'M' ) {
开发者_JS百科 my $event_type = ord( ReadKey 0, $tty_in ) - 32;
my $x = ord( ReadKey 0, $tty_in ) - 32;
my $y = ord( ReadKey 0, $tty_in ) - 32;
return $x, $y;
}
}
}
}
#--------------------------------------------------------------
open my $tty_in, '<:encoding(utf-8)', '/dev/tty' or die $!;
Term::ReadKey::ReadMode 'ultra-raw', $tty_in;
# enter_mouse_mode
close $tty_in;
open $tty_in, '<:bytes', '/dev/tty' or die $!;
print "\e[?1003h"; # sets SET_ANY_EVENT_MOUSE mode
my( $x, $y ) = getch( $tty_in );
# leave_mouse_mode
close $tty_in;
open $tty_in, '<:encoding(utf-8)', '/dev/tty' or die $!;
print "\e[?1003l"; # cancels SET_ANY_EVENT_MOUSE mode
Term::ReadKey::ReadMode 'restore', $tty_in;
close $tty_in;
#----------------------------------------------------------------
say "x = $x";
say "y = $y";
I tried to move the marked part from "open $tty_in" to "STDIN", but it doesn't work yet. When "x" gets bigger then "95"(127-32) I get an error-message (utf8 "\x80" does not map to Unicode at (eval 1) line ...). How do I have to modify the replacement-part to get it work?
binmode STDIN, ':encoding(utf-8)' or die $!;
Term::ReadKey::ReadMode 'ultra-raw';
# enter_mouse_mode
binmode STDIN, ':bytes' or die $!;
print "\e[?1003h";
my( $x, $y ) = getch( *STDIN );
# leave_mouse_mode
binmode STDIN, ':encoding(utf-8)' or die $!;
print "\e[?1003l";
Term::ReadKey::ReadMode 'restore';
I have to change
binmode STDIN, ':bytes' or die $!;
with
binmode STDIN, ':pop:bytes' or die $!;
or with
binmode STDIN, ':raw' or die $!;
then it works.
精彩评论