AnyEve...TermKey(3pm)

πŸ“¦
version 0.02 Β· 2 versions
  • 0.02 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.02 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ AnyEvent::TermKey ✨

okay bestie, welcome to AnyEvent::TermKey πŸ’– β€” it's the terminal key input situation using libtermkey with AnyEvent. she's asynchronous, she's abstract, she's giving very demure very mindful keypress handling. no cap. πŸŽ€


🌸 SYNOPSIS 🌸

use AnyEvent::TermKey qw( FORMAT_VIM KEYMOD_CTRL );
use AnyEvent;

my $cv = AnyEvent->condvar;

my $aetk = AnyEvent::TermKey->new(
   term => \*STDIN,

   on_key => sub {
      my ( $key ) = @_;

      print "Got key: ".$key->termkey->format_key( $key, FORMAT_VIM )."\n";

      $cv->send if $key->type_is_unicode and
                   $key->utf8 eq "C" and
                   $key->modifiers & KEYMOD_CTRL;
   },
);

$cv->recv;

πŸ¦‹ DESCRIPTION πŸ¦‹

so basically πŸ’… this class is an asynchronous perl wrapper around the libtermkey library. and what does libtermkey do? it gives you an abstract lil way to read keypress events in terminal-based programs. the whole point is it hands you structures that describe keys instead of just dumping raw bytes at you like the TTY device does. it's giving ✨glow up✨ compared to raw reads, fr fr.

internally it uses an instance of Term::TermKey to get at the underlying C library. and for the deets on how it all operates β€” including how keypress events get represented as objects β€” you wanna check the docs on that class. iykyk πŸ“š

there's also proxy methods for the normal accessors of Term::TermKey, and the usual behaviour of getkey or whatever other methods is instead replaced by the on_key event. so like, don't go looking for getkey to behave normally, bestie, that's not the vibe here 🫢


πŸ‘‘ CONSTRUCTOR πŸ‘‘

$aetk = AnyEvent::TermKey->new( %args )

this function returns a shiny new instance of a AnyEvent::TermKey object ✨ and it takes these named arguments, mwah:

  • term => IO or INT β€” optional πŸ“ it's the file handle, or the POSIX file descriptor number, for the file handle you wanna use as the connection to the terminal. if you don't supply it, STDIN gets used. default behaviour, iconic.
  • on_key => CODE β€” a CODE reference to the key-event handling callback πŸ’– will be passed an instance of a Term::TermKey::Key structure:
$on_key->( $key )

πŸ’œ METHODS πŸ’œ

$tk = $aetk->termkey

returns the Term::TermKey object that's being used to access the libtermkey library. ngl you normally shouldn't need this one β€” the proxy methods are the move instead. see below πŸ‘‡

$flags = $aetk->get_flags
$aetk->set_flags( $flags )
$canonflags = $aetk->get_canonflags
$aetk->set_canonflags( $canonflags )
$msec = $aetk->get_waittime
$aetk->set_waittime( $msec )
$str = $aetk->get_keyname( $sym )
$sym = $aetk->keyname2sym( $keyname )
( $ev, $button, $line, $col ) = $aetk->interpret_mouse( $key )
$str = $aetk->format_key( $key, $format )
$key = $aetk->parse_key( $str, $format )
$key = $aetk->parse_key_at_pos( $str, $format )
$cmp = $aetk->keycmp( $key1, $key2 )

all of these methods proxy straight to the Term::TermKey object 🫢 which means you can transparently use the AnyEvent::TermKey object as if it was a subclass. their arguments, behaviour and return value are therefore exactly whatever that class provides. for more detail, go read the Term::TermKey documentation. periodt πŸ’…


πŸŽ€ AUTHOR πŸŽ€

Paul Evans <leonerd@leonerd.org.uk> β€” the icon, the legend, the main character πŸ‘‘


and that's the whole page bestie, we ate and left no crumbs πŸ“ this was perl v5.36.0, dated 2022-12-06, and it's from AnyEve...TermKey(3pm) β€” go forth and read them keys βœ¨πŸ’–πŸ¦‹

πŸ“¦ where this page comes from