在konsole上读取鼠标事件

发布于 2024-10-12 06:31:16 字数 1727 浏览 2 评论 0原文

#!/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';
#!/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";

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';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2024-10-19 06:31:17

我必须改变

binmode STDIN, ':bytes' or die $!;

with

binmode STDIN, ':pop:bytes' or die $!;

或 with

binmode STDIN, ':raw' or die $!;

然后它就起作用了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文