AnyEvent-TermKey
view release on metacpan or search on metacpan
lib/AnyEvent/TermKey.pm view on Meta::CPAN
goto &$import; # So as not to have to fiddle with Sub::UpLevel
}
=head1 CONSTRUCTOR
=cut
=head2 $aetk = AnyEvent::TermKey->new( %args )
This function returns a new instance of a C<AnyEvent::TermKey> object. It
takes the following named arguments:
=over 8
=item term => IO or INT
Optional. File handle or POSIX file descriptor number for the file handle to
use as the connection to the terminal. If not supplied C<STDIN> will be used.
=item on_key => CODE
CODE reference to the key-event handling callback. Will be passed an instance
of a C<Term::TermKey::Key> structure:
$on_key->( $key )
=back
=cut
sub new
{
my $class = shift;
my %args = @_;
# TODO: Find a better algorithm to hunt my terminal
my $term = delete $args{term} || \*STDIN;
my $on_key = $args{on_key};
my $termkey = Term::TermKey->new( $term, delete $args{flags} || 0 );
if( !defined $termkey ) {
croak "Cannot construct a termkey instance\n";
}
my $timeout;
my $iowatch = AnyEvent->io(
fh => $term,
poll => "r",
cb => sub {
undef $timeout;
return unless $termkey->advisereadable == RES_AGAIN;
my $ret;
while( ( $ret = $termkey->getkey( my $key ) ) == RES_KEY ) {
$on_key->( $key );
}
if( $ret == RES_AGAIN ) {
$timeout = AnyEvent->timer(
after => $termkey->get_waittime / 1000,
cb => sub {
if( $termkey->getkey_force( my $key ) == RES_KEY ) {
$on_key->( $key );
}
},
);
}
},
);
return bless {
termkey => $termkey,
iowatch => $iowatch,
on_key => $args{on_key},
}, $class;
}
=head1 METHODS
=cut
=head2 $tk = $aetk->termkey
Returns the C<Term::TermKey> object being used to access the C<libtermkey>
library. Normally should not be required; the proxy methods should be used
instead. See below.
=cut
sub termkey
{
my $self = shift;
return $self->{termkey};
}
=head2 $flags = $aetk->get_flags
=head2 $aetk->set_flags( $flags )
=head2 $canonflags = $aetk->get_canonflags
=head2 $aetk->set_canonflags( $canonflags )
=head2 $msec = $aetk->get_waittime
=head2 $aetk->set_waittime( $msec )
=head2 $str = $aetk->get_keyname( $sym )
=head2 $sym = $aetk->keyname2sym( $keyname )
=head2 ( $ev, $button, $line, $col ) = $aetk->interpret_mouse( $key )
=head2 $str = $aetk->format_key( $key, $format )
=head2 $key = $aetk->parse_key( $str, $format )
=head2 $key = $aetk->parse_key_at_pos( $str, $format )
( run in 0.865 second using v1.01-cache-2.11-cpan-62a16548d74 )