#!/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' ) {
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";
我试图将标记的部分从“open $tty_in”移动到“STDIN”,但它还不起作用。当“x”变得大于“95”(127-32)时,我收到一条错误消息(utf8“\x80”在 (eval 1) 行不映射到 Unicode ...)。我如何修改替换零件才能使其正常工作?
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';