List Info

Thread: Does the Glib mainloop have the ability to capture keypresses?




Does the Glib mainloop have the ability to capture keypresses?
country flaguser name
United States
2007-02-14 16:50:16
Hi,
I have plenty of examples for Gtk2 windows, and capturing
keyboard events, but I hav'nt found anything for Glib
alone,
for use in commandline loops.

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?

Thanks,
zentara


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.
html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Does the Glib mainloop have the ability to capture keypresses?
country flaguser name
United States
2007-02-14 17:18:29
On Feb 14, 2007, at 5:50 PM, zentara wrote:

> I have plenty of examples for Gtk2 windows, and
capturing
> keyboard events, but I hav'nt found anything for Glib
alone,
> for use in commandline loops.

That's because the keypresses that gtk+ deals with are
simply X  
events, not characters on a serial terminal stream.


> 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.


--
If the monkey could type one keystroke every nanosecond, the
expected  
waiting time until the monkey types out Hamlet is so long
that the  
estimated age of the universe is insignificant by comparison
... this  
is not a practical method for writing plays.
   -- Gian-Carlo Rota


_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Does the Glib mainloop have the ability to capture keypresses?
country flaguser name
United States
2007-02-15 14:54:05
On Wed, 14 Feb 2007 18:18:29 -0500
muppet <scottasofyet.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;                                        
                          

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
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )