note
zentara
It smells like a homework assignment to me, but here are some things you will need to consider. <p> First, in order to have your program be both responsive to key entries, and at the same time be running other subroutines, you will need to use fork or threads. <p> Second, if you want to enter data like AB<ENTER>, you will be looking for readline routines, not readkey routines. Readline waits for an <ENTER> before sending it to STDIN.<p>Here are a few basic examples. There are many other event systems besides Glib, but I prefer it.
<c>
#!/usr/bin/perl
use warnings;
use strict;
use Glib;
my $main_loop = Glib::MainLoop->new;
Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback, 'STDIN');
#just to show it's non blocking
my $timer1 = Glib::Timeout->add (10000, \&testcallback, undef, 1 );
$main_loop->run;
sub watch_callback {
# print "@_\n";
my ($fd, $condition, $fh) = @_;
my $line = readline STDIN;
print $line;
#always return TRUE to continue the callback
return 1;
}
sub testcallback{
print "\t\t\t".time."\n";
}
__END__
</c>
<c>
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
use threads;
$|++;
#ReadMode('cbreak');
# works non-blocking if read stdin is in a thread
my $thr = threads->new(\&read_in)->detach;
while(1){
print "test\n";
sleep 5;
}
#ReadMode('normal'); # restore normal tty settings
sub read_in{
while(1){
my $char;
if (defined ($char = ReadKey(0)) ) {
print "\t\t$char->", ord($char),"\n";
#process key presses here
if($char eq 'q'){exit}
#if(length $char){exit} # panic button on any key :-)
}
}
}
__END__
</c>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-131741">
<hr />
I'm not really a human, but I play one on earth.<br>
[id://630805] ................... <a href=http://zentara.net/japh.html> flash japh </a>
</div></div>
1008451
1008451