in reply to Is it possible in perl to detect keyboard events like as "when keydown"

I'm not sure if you can extend this to "keydown/keyup", you may need Gtk2 (a gui) for that. But this is a commandline eventloop using GLib that will record keys.

The nice thing about the Glib eventloop, is you can add timers, and filehandle watches to it, to accomplish most anything. If you seek more examples, google for "Glib keypress".

#!/usr/bin/perl use warnings; use strict; use Glib; use Glib qw/TRUE FALSE/; # thanks to muppet for this use Term::ReadKey; $|++; ReadMode('cbreak'); my $main_loop = Glib::MainLoop->new; Glib::Idle->add( sub{ my $char; if (defined ($char = ReadKey(0)) ) { print "$char->", ord($char),"\n"; #process key presses here } return TRUE; #keep this going }); $main_loop->run; ReadMode('normal'); # restore normal tty settings __END__

I'm not really a human, but I play one on earth. Cogito ergo sum a bum
  • Comment on Re: Is it possible in perl to detect keyboard events like as "when keydown"
  • Download Code