如何启用扩展鼠标模式?

发布于 2024-10-12 10:40:18 字数 1257 浏览 1 评论 0原文

#!/usr/bin/env perl
use warnings;
use 5.012; 
use Term::ReadKey;

sub getch {
    my $c = ReadKey 0;
    if ( $c eq "\e" ) {
        my $c = ReadKey 0.10;
        if ( $c eq '[' ) {
            my $c = ReadKey 0;
            if ( $c eq 'M' ) {   
                my $event_type = ord( ReadKey 0 ) - 32;
                my $x = ord( ReadKey 0 ) - 32;
                my $y = ord( ReadKey 0 ) - 32;
                return $x, $y;
} } } }

binmode STDIN, ':encoding(utf-8)' or die $!;
ReadMode 'ultra-raw';

# enter_mouse_mode
binmode STDIN, ':raw' or die $!;
print "\e[?1003h"; # sets   SET_ANY_EVENT_MOUSE  mode

my( $x, $y  ) = getch();

# leave_mouse_mode
binmode STDIN, ':encoding(utf-8)' or die $!;
print "\e[?1003l"; # cancels SET_ANY_EVENT_MOUSE mode

ReadMode 'restore';
say "x = $x";
say "y = $y";

该脚本最多只能工作 223 列宽(223 + 32 -> 1 字节)。
有谁知道如何启用扩展鼠标模式? 我尝试以这种方式更改鼠标模式,但它不起作用:

# enter_mouse_mode
binmode STDIN, ':utf8' or die $!;
print "\e[?1003h";
print "\e[?1005h"; ###

my( $x, $y  ) = getch();

# leave_mouse_mode
binmode STDIN, ':encoding(utf-8)' or die $!;
print "\e[?1003l";
print "\e[?1005l"; ###
#!/usr/bin/env perl
use warnings;
use 5.012; 
use Term::ReadKey;

sub getch {
    my $c = ReadKey 0;
    if ( $c eq "\e" ) {
        my $c = ReadKey 0.10;
        if ( $c eq '[' ) {
            my $c = ReadKey 0;
            if ( $c eq 'M' ) {   
                my $event_type = ord( ReadKey 0 ) - 32;
                my $x = ord( ReadKey 0 ) - 32;
                my $y = ord( ReadKey 0 ) - 32;
                return $x, $y;
} } } }

binmode STDIN, ':encoding(utf-8)' or die $!;
ReadMode 'ultra-raw';

# enter_mouse_mode
binmode STDIN, ':raw' or die $!;
print "\e[?1003h"; # sets   SET_ANY_EVENT_MOUSE  mode

my( $x, $y  ) = getch();

# leave_mouse_mode
binmode STDIN, ':encoding(utf-8)' or die $!;
print "\e[?1003l"; # cancels SET_ANY_EVENT_MOUSE mode

ReadMode 'restore';
say "x = $x";
say "y = $y";

This script works only up to 223 columns wide ( 223 + 32 -> 1 byte ).
Does anybody know, how to enable the extended mouse mode?
I tried to change the mouse-mode this way, but it didn't work:

# enter_mouse_mode
binmode STDIN, ':utf8' or die $!;
print "\e[?1003h";
print "\e[?1005h"; ###

my( $x, $y  ) = getch();

# leave_mouse_mode
binmode STDIN, ':encoding(utf-8)' or die $!;
print "\e[?1003l";
print "\e[?1005l"; ###

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

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

发布评论

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

评论(2

爱情眠于流年 2024-10-19 10:40:18

您使用什么版本的 xterm?根据 changelog,最近添加了扩展鼠标模式(版本 262) 。你的代码对我来说适用于 xterm 266,perl 5.10。我无法通过一些简单的策略(LANG=C xtermxterm +lc 禁用区域设置支持)来打破它。

What version of xterm are you using? According to the changelog, extended mouse mode was added recently (version 262). Your code worked for me with xterm 266, perl 5.10. I wasn't able to break it with a few simple strategies (LANG=C xterm, xterm +lc to disable locale support).

甲如呢乙后呢 2024-10-19 10:40:18

我建议不要使用扩展鼠标模式,1005。它无法与常规(X10)编码区分开,并且仍然存在无法分辨释放事件涉及哪个按钮的问题。

相反,我建议您的终端是否支持它,使用 SGR 编码,模式 1006。

另请参阅我最近关于终端鼠标编码的来龙去脉的博客文章:

http://leonerds-code.blogspot.co.uk/2012/04/wide-mouse-support -in-libvterm.html

使用 SGR 编码的好处是您可以尝试启用它,但不需要知道它是否成功;从终端返回的字节告诉你这一点。然而,您永远不会知道扩展 (UTF-8) 模式是否已成功启用,但您需要知道这一点才能正确解释返回的字节。

I recommend not using extended mouse mode, 1005. It is impossible to distinguish from regular (X10) encoding, and it still suffers the problem that you cannot tell which button is involved in a release event.

Instead, I'd suggest if your terminal supports it, using SGR encoding, mode 1006.

See also my recent blog post entry on the ins and outs of terminal mouse encodings:

http://leonerds-code.blogspot.co.uk/2012/04/wide-mouse-support-in-libvterm.html

The benefit of using SGR encoding is that you can attempt to enable it but you don't need to know if it was successful; the returned bytes from the terminal tell you this. Whereas, you'll never know if extended (UTF-8) mode was enabled successfully, but you need to know this in order to interpret the returned bytes correctly.

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