Here is the basic idea of how i would do it..... but it uses an event loop system
## from previous posts ###############
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
Re: Does the Glib mainloop have the ability to capture keypresses?
by muppet
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.
</muppet>
me.......
I came up with 2 different versions. The first will trigger the callba
+ck
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-l
+ine
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__
|