File: simple.pl

package info (click to toggle)
libterm-termkey-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: perl: 370; makefile: 5
file content (46 lines) | stat: -rw-r--r-- 1,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/perl

use v5.14;
use warnings;

use Getopt::Long;
use Term::TermKey qw( FLAG_UTF8 RES_EOF FORMAT_VIM FORMAT_MOUSE_POS );

my $mouse;
GetOptions(
   'm|mouse=i' => \$mouse
) or exit(1);

$|++;

if( $mouse ) {
   print "\e[?${mouse}h";
}

$SIG{WINCH} = sub { print "Window resize\n" };

my $tk = Term::TermKey->new(\*STDIN);

# ensure perl and libtermkey agree on Unicode handling
binmode( STDOUT, ":encoding(UTF-8)" ) if $tk->get_flags & FLAG_UTF8;

while( ( my $ret = $tk->waitkey( my $key ) ) != RES_EOF ) {
   if( $key->type_is_mouse ) {
      printf "Got mouse: %s(%d) at (%d,%d)\n", [qw( * press drag release )]->[$key->mouseev],
         $key->button, $key->line, $key->col;
   }
   elsif( $key->type_is_position ) {
      printf "Got position report: at (%d,%d)\n", $key->line, $key->col;
   }
   else {
      print "Got key: ".$tk->format_key( $key, FORMAT_VIM )."\n";

      if( $key->type_is_unicode && !$key->modifiers && $key->utf8 eq "?" ) {
         print "\e[6n";
      }
   }
}

if( $mouse ) {
   print "\e[?${mouse}l";
}