I know it works in a non-blocking fashion if I call
$char = ReadKey(-1);
However I'll still have to call that like every 0.5 seconds in callbacks of an event-based timer, so it is not exactly real-time. I can improve its responsiveness to 0.2 seconds maybe but here's hoping there is a more perfect solution.
| [reply] [d/l] |
I can improve its responsiveness to 0.2 seconds maybe
There is nothing to stop you calling it every 0.001 seconds if you think the typist is likely to enter something significant within that time. Of course, the problem with this kind of architecture is that you have to do you own line buffering, getline-style editing, history etc.
However, here's a more convenient alternative. It uses a thread to send any input from STDIN to a selectable socket. With the advantages that a) you don't need to set up callbacks or artificial timers; b) all the standard command line handling is available to the user; c) line buffering and re-assembly is taken care of:
#! perl -slw
use strict;
use threads;
use IO::Socket;
use IO::Select;
$|++;
sub selectableSTDIN {
my $true = 1;
my $in = IO::Socket::INET->new(
LocalAddr => '127.0.0.1:65521',
Proto => "udp",
) or die "Failed to bind port 65521";
ioctl( $in, 0x8004667e, \$true ) or die $!;
my $out = IO::Socket::INET->new(
PeerAddr => '127.0.0.1:65521',
Proto => "udp"
) or die "Failed to bind remote port 65521";
async {
$out->send( $_ ) while <STDIN>;
}->detach;
return $in;
}
my $sel = IO::Select->new( selectableSTDIN() );
while( 1 ) {
for my $src ( $sel->can_read( 0.1 ) ) {
print "\n", scalar <$src>;
}
printf '.';
}
__END__
C:\test>selectableSTDIN.pl
...........f.r..e.d..
fred
........b..i....l.l...
bill
..........j..o..h..n...
.
john
.......j.a...c...k...
jack
........Terminating on signal SIGINT(2)
-
-
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Glib works on Windows. Try this:
#!/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, 'STDI
+N');
#just to show it's non blocking
my $timer1 = Glib::Timeout->add (1000, \&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__
| [reply] [d/l] |
After installing ExtUtils::Depends, ExtUtils::PkgConfig and Glib, I ran the code but was greeted with an error popup like this:
The procedure entry point g_initially_unowned_get_type could not be located in the dynamic link library libgobject-2.0-0.dll.
| [reply] |
It sounds like you have a version mismatch between the glib dll you have, and the Perl module.... its a common problem installing c-based modules on win32. If you want an easy way to get it going, put in Camelbox: A build of Gtk2-Perl for Windows , although that will give you a full Gtk2 install, in addition to the Glib base object library.
| [reply] |
| [reply] |
| [reply] |