Greetings Monks,
Ok, I'm trying to do this Console/Teminal interaction shell... So far I've done the following which is the closest I've gotten to what I want:
#!/usr/bin/perl
use Term::ReadKey;
use strict;
use warnings;
startListening();
#$| = 1;
#$|++;
#select(STDOUT);
#$| = 1;
sub startListening {
# Disable CTRL keys
ReadMode(4);
my $word = '';
print "> ";
while(1) {
my $char;
if (defined ($char = ReadKey(0))){
print $char;
}
my $ord = ord($char);
last if ($ord == 3);
if ($ord == 10 or $ord == 13) { # "Enter"
enterPressed($word);
$word = "";
print "> ";
next;
}
elsif ($ord == 127) { # ie "Backspace"
#chop($word);
#print chr(8),' ',chr(8);
print "BACKSPACE!\n";
next;
}
elsif ($ord == 27) {
#clearstream();
#$ord="\0";
}
elsif ($ord == 8) {
print "BACKSPACE!\n";
}
#if($ord != "\0"){
#$word .= chr($ord);
#print '*';
#}
}
ReadMode(0);
}
sub clearstream {
while(1) {
my $char;
if (defined ($char = ReadKey(-1)) ){}else{return}
}
return;
}
sub enterPressed {
my $word = shift;
print "WORD: " . $word . "\n";
}
Now, the problem I have here running this in Window XP, is that I have to press another key (several times) just after a Enter/Return press to show the "WORD..." output. Why is that and how to fix this? Seems like its cached or not flushed properly?
I've tested out with ReadMode(0); but then I can't make the "BACKSPACE!" work. Although I may not need all those special keys, it would be nice to make it work just in case...
Oh, and yea, I know about Term::ReadLine and all those Zoid and Gnu submodules... but Zoid didn't really work in Windows... Got some wierd output by keypress which I shouldn't get... And, with ReadKey its not that big step to make it work as ReadLine (that is, save all keypresses in a scalar, and use this "command" saved in a scalar when enter is pressed)...plus that ReadKey can be non-blocking aswell!
Thanks,
Ace
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.