Search code examples
perlencodingmouseeventlibtermkey

How to enable extended mouse mode?


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

Solution

  • 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).