Question

#!/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"; ###
Was it helpful?

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top