#!/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) よりも大きくなると、エラー メッセージが表示されます ((eval 1) 行で utf8 "\x80" が 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';