Re: Does the Glib mainloop have the ability to capture keypresses?
- From: zentara <zentara1 sbcglobal net>
- To: gtk-perl-list gnome org
- Subject: Re: Does the Glib mainloop have the ability to capture keypresses?
- Date: Thu, 15 Feb 2007 15:54:05 -0500
On Wed, 14 Feb 2007 18:18:29 -0500
muppet <scott asofyet org> wrote:
zentara wrote:
I can put
Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback);
and watch what is input on STDIN, but is that the best way?
I took that approach in gish, where i faked out the Tk portion of  
Readline.
http://asofyet.org/muppet/software/gtk2-perl/gish.html
But it is important that you set up STDIN properly to give you all  
the characters pressed, instead of using line-buffering.  Readline  
does that for you...  in your own app, you'll have to use the termios  
stuff from the POSIX module.
I came up with 2 different versions. The first will trigger the callback
on an enter press, to take whole line commands.
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
use Glib qw/TRUE FALSE/;
use FileHandle;
#accepts enter to provide input
my $fh_in = FileHandle->new();
$fh_in = *STDIN{IO};                                                                   
my $main_loop = Glib::MainLoop->new;
Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback);       
$main_loop->run;
######################################
sub watch_callback {
  while (<$fh_in>) {                                                                        
    my $line = $_;
    if (defined($line) and $line ne ''){
          print $line;
    }
  }
return 0;
}
__END__
The above waits for you to type, then press enter, to get a whole line.
Without the FileHandle module, you could still send commands to STDIN,
but you needed a Control-d to send it. It would however, allow multi-line
input.
###############################################################
This one will capture just a key-press, which you can process.
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
use Glib qw/TRUE FALSE/;
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__
zentara
-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]