How to get a keypress?
am 15.09.2007 00:24:34 von Lars Eighner
The ncurses functions are such a mess (and some of it I suspect is in
ncurses itself) that I am thinking the easiest way to write a terminal
interface is simply to write ANSI directly to the terminal.
Although the manual on the CLI refers me to readline, readline doesn't
seem provide a way to get a single key press, and so far as I can tell
doesn't provide a reference to how to get a single keypress. I reckon
some widgets would be more spiffy with keypresses instead of whole lines.
How can I get keypresses?
--
Lars Eighner
Countdown: 493 days to go.
What do you do when you're debranded?
Re: How to get a keypress?
am 15.09.2007 01:21:48 von Norman Peelman
Lars Eighner wrote:
> The ncurses functions are such a mess (and some of it I suspect is in
> ncurses itself) that I am thinking the easiest way to write a terminal
> interface is simply to write ANSI directly to the terminal.
>
> Although the manual on the CLI refers me to readline, readline doesn't
> seem provide a way to get a single key press, and so far as I can tell
> doesn't provide a reference to how to get a single keypress. I reckon
> some widgets would be more spiffy with keypresses instead of whole lines.
>
> How can I get keypresses?
>
If you're on Windows, you can't. You have to press the enter key to
'send' the input. Unix/Linux it's supposed to work as expected. Haven't
tried it yet though.
Norm
Re: How to get a keypress?
am 15.09.2007 01:38:28 von Lars Eighner
In our last episode, <46eb178b$0$32540$4c368faf@roadrunner.com>, the lovely
and talented Norman Peelman broadcast on comp.lang.php:
> Lars Eighner wrote:
>> The ncurses functions are such a mess (and some of it I suspect is in
>> ncurses itself) that I am thinking the easiest way to write a terminal
>> interface is simply to write ANSI directly to the terminal.
>>
>> Although the manual on the CLI refers me to readline, readline doesn't
>> seem provide a way to get a single key press, and so far as I can tell
>> doesn't provide a reference to how to get a single keypress. I reckon
>> some widgets would be more spiffy with keypresses instead of whole lines.
>>
>> How can I get keypresses?
>>
> If you're on Windows, you can't. You have to press the enter key to
> 'send' the input. Unix/Linux it's supposed to work as expected. Haven't
> tried it yet though.
What is "it's" in "it's supposed to work as expected"?
On FreeBSD readline will not capture key presses (or at least I do not know
how to do so). If I initiate ncurses to use ncurses_getch, it seizes the
display so cannot write ANSI to it (and it overwrites any ANSI I have
written to the screen --- besides which, the whole point of writing an ANSI
interface is to avoid PHP ncurses which are (is) ugly as hell, largely
undocumented, inflexible, object-oriented, and tends to crash the terminal
rather than failing gracefully when it fails.
It is easy enough to draw widgets by writing ANSI directly to a terminal,
but if you cannot move the cursor around with the arrow key or open a
drop-down box with Enter, the widgets are just pretty pictures.
--
Lars Eighner
Countdown: 493 days to go.
What do you do when you're debranded?
Re: How to get a keypress?
am 15.09.2007 01:47:33 von Jim Carlock
Lars Eighner asked...
> How can I get keypresses?
"Norman Peelman" replied...
: If you're on Windows, you can't.
HTML provides a way to get a keypress. Usually this can be
done with javascript or HTML inside the
section or
HTML inside the body. I've not messed with it, but have seen
it in other folks code.
If he just wants a way to provide client-side control, it's
called accesskey in HTML-speak.
http://www.w3.org/TR/html4/interact/forms.html#h-17.11.2
That combined with some javascript to do an automatic POST,
probably can get the OP where he wants to go.
--
Jim Carlock
Swimming Pool, Spa And Water Feature Builders
http://www.aquaticcreationsnc.com/
Re: How to get a keypress?
am 15.09.2007 01:51:09 von Lars Eighner
In our last episode, , the
lovely and talented Lars Eighner broadcast on comp.lang.php:
> In our last episode, <46eb178b$0$32540$4c368faf@roadrunner.com>, the lovely
> and talented Norman Peelman broadcast on comp.lang.php:
>> Lars Eighner wrote:
>>> The ncurses functions are such a mess (and some of it I suspect is in
>>> ncurses itself) that I am thinking the easiest way to write a terminal
>>> interface is simply to write ANSI directly to the terminal.
>>>
>>> Although the manual on the CLI refers me to readline, readline doesn't
>>> seem provide a way to get a single key press, and so far as I can tell
>>> doesn't provide a reference to how to get a single keypress. I reckon
>>> some widgets would be more spiffy with keypresses instead of whole lines.
>>>
>>> How can I get keypresses?
>>>
>> If you're on Windows, you can't. You have to press the enter key to
>> 'send' the input. Unix/Linux it's supposed to work as expected. Haven't
>> tried it yet though.
> What is "it's" in "it's supposed to work as expected"?
> On FreeBSD readline will not capture key presses (or at least I do not know
> how to do so). If I initiate ncurses to use ncurses_getch, it seizes the
> display so cannot write ANSI to it (and it overwrites any ANSI I have
> written to the screen --- besides which, the whole point of writing an ANSI
> interface is to avoid PHP ncurses which are (is) ugly as hell, largely
> undocumented, inflexible, object-oriented, and tends to crash the terminal
> rather than failing gracefully when it fails.
> It is easy enough to draw widgets by writing ANSI directly to a terminal,
> but if you cannot move the cursor around with the arrow key or open a
> drop-down box with Enter, the widgets are just pretty pictures.
Opps! Never mind. readline() is not suitable for this purpose,
but at least in recent versions of PHP fread will work:
$keypress = fread(STDIN,1);
gets one key from STDIN (and at least in recent versions, the standard
streams are preopened). I have not yet investigated whether or how to flush
STDIN in case of multiple (mistaken) keystrokes, but I'm sure this is the
right track.
--
Lars Eighner
Countdown: 493 days to go.
What do you do when you're debranded?
Re: How to get a keypress?
am 15.09.2007 06:46:51 von Norman Peelman
Jim Carlock wrote:
> Lars Eighner asked...
>> How can I get keypresses?
>
> "Norman Peelman" replied...
> : If you're on Windows, you can't.
>
> HTML provides a way to get a keypress. Usually this can be
> done with javascript or HTML inside the
section or
> HTML inside the body. I've not messed with it, but have seen
> it in other folks code.
>
> If he just wants a way to provide client-side control, it's
> called accesskey in HTML-speak.
>
> http://www.w3.org/TR/html4/interact/forms.html#h-17.11.2
>
> That combined with some javascript to do an automatic POST,
> probably can get the OP where he wants to go.
>
The OP is in a terminal (cli) window... no browser.
Norm
Re: How to get a keypress?
am 15.09.2007 07:06:37 von Lars Eighner
In our last episode, <46eb63bb$0$28796$4c368faf@roadrunner.com>, the lovely
and talented Norman Peelman broadcast on comp.lang.php:
> Jim Carlock wrote:
>> Lars Eighner asked...
>>> How can I get keypresses?
>>
>> "Norman Peelman" replied...
>> : If you're on Windows, you can't.
>>
>> HTML provides a way to get a keypress. Usually this can be
>> done with javascript or HTML inside the
section or
>> HTML inside the body. I've not messed with it, but have seen
>> it in other folks code.
>>
>> If he just wants a way to provide client-side control, it's
>> called accesskey in HTML-speak.
>>
>> http://www.w3.org/TR/html4/interact/forms.html#h-17.11.2
>>
>> That combined with some javascript to do an automatic POST,
>> probably can get the OP where he wants to go.
>>
> The OP is in a terminal (cli) window... no browser.
And no HTML, Javascript. There is simply no difficulty in doing this
with served PHP browsed with, say, lynx --- and for that matter doing it
without a server using lynxexec. I'm talking about doing this directly
with a terminal without server/browser overhead and without the both
of POST etc.
And I was wrong that fread will do it. I'm still looking for a way to
get key presses without having to hit return and without their echoing
to the terminal.
Look, the following will make a perfectly acceptable box in a (fullscreen)
window. The problem is what to write for the commented line that will
give me the value of the key pressed and not mess up the pretty screen.
#!/usr/local/bin/php
print "\x1b[2J\x1b[=3A";
window(0,0,80,25,7);
border(10,10,70,20,4);
// See if UpArrow is pressed:
// ??? $keypress = fread(STDIN,1); DOESN'T WORK
// if ($keypress = "\x1b[A"){ print "yes";}
function window($minx,$miny,$maxx,$maxy,$bgc){
for ($i = $minx; $i < $maxx + 1; $i++){
for ($j = $miny; $j < $maxy +1; $j++){
print "\x1b[$j;{$i}f";
print "\x1b[1;4{$bgc}m ";
}
}
function border($minx,$miny,$maxx,$maxy,$bgc){
$j=$miny;
for ($i = $minx; $i < $maxx + 1; $i++){
print "\x1b[$j;{$i}f";
print "\x1b[1;4{$bgc}m ";
}
$j=$maxy;
for ($i = $minx; $i < $maxx + 1; $i++){
print "\x1b[$j;{$i}f";
print "\x1b[1;4{$bgc}m ";
}
$i=$minx;
for ($j = $miny + 1; $j < $maxy; $j++){
print "\x1b[$j;{$i}f";
print "\x1b[1;4{$bgc}m ";
}
$i=$maxx;
for ($j = $miny + 1; $j < $maxy; $j++){
print "\x1b[$j;{$i}f";
print "\x1b[1;4{$bgc}m ";
}
}
}
?>
--
Lars Eighner
Countdown: 493 days to go.
What do you do when you're debranded?
Re: How to get a keypress?
am 15.09.2007 13:35:12 von Norman Peelman
Lars Eighner wrote:
> In our last episode, <46eb63bb$0$28796$4c368faf@roadrunner.com>, the lovely
> and talented Norman Peelman broadcast on comp.lang.php:
>
>> Jim Carlock wrote:
>>> Lars Eighner asked...
>>>> How can I get keypresses?
>>> "Norman Peelman" replied...
>>> : If you're on Windows, you can't.
>>>
>>> HTML provides a way to get a keypress. Usually this can be
>>> done with javascript or HTML inside the
section or
>>> HTML inside the body. I've not messed with it, but have seen
>>> it in other folks code.
>>>
>>> If he just wants a way to provide client-side control, it's
>>> called accesskey in HTML-speak.
>>>
>>> http://www.w3.org/TR/html4/interact/forms.html#h-17.11.2
>>>
>>> That combined with some javascript to do an automatic POST,
>>> probably can get the OP where he wants to go.
>>>
>
>> The OP is in a terminal (cli) window... no browser.
>
> And no HTML, Javascript. There is simply no difficulty in doing this
> with served PHP browsed with, say, lynx --- and for that matter doing it
> without a server using lynxexec. I'm talking about doing this directly
> with a terminal without server/browser overhead and without the both
> of POST etc.
>
> And I was wrong that fread will do it. I'm still looking for a way to
> get key presses without having to hit return and without their echoing
> to the terminal.
>
> Look, the following will make a perfectly acceptable box in a (fullscreen)
> window. The problem is what to write for the commented line that will
> give me the value of the key pressed and not mess up the pretty screen.
>
> #!/usr/local/bin/php
>
>
> print "\x1b[2J\x1b[=3A";
> window(0,0,80,25,7);
> border(10,10,70,20,4);
>
> // See if UpArrow is pressed:
> // ??? $keypress = fread(STDIN,1); DOESN'T WORK
> // if ($keypress = "\x1b[A"){ print "yes";}
>
>
> function window($minx,$miny,$maxx,$maxy,$bgc){
> for ($i = $minx; $i < $maxx + 1; $i++){
> for ($j = $miny; $j < $maxy +1; $j++){
> print "\x1b[$j;{$i}f";
> print "\x1b[1;4{$bgc}m ";
> }
> }
> function border($minx,$miny,$maxx,$maxy,$bgc){
> $j=$miny;
> for ($i = $minx; $i < $maxx + 1; $i++){
> print "\x1b[$j;{$i}f";
> print "\x1b[1;4{$bgc}m ";
> }
> $j=$maxy;
> for ($i = $minx; $i < $maxx + 1; $i++){
> print "\x1b[$j;{$i}f";
> print "\x1b[1;4{$bgc}m ";
> }
> $i=$minx;
> for ($j = $miny + 1; $j < $maxy; $j++){
> print "\x1b[$j;{$i}f";
> print "\x1b[1;4{$bgc}m ";
> }
> $i=$maxx;
> for ($j = $miny + 1; $j < $maxy; $j++){
> print "\x1b[$j;{$i}f";
> print "\x1b[1;4{$bgc}m ";
> }
> }
>
> }
> ?>
>
>
Take a look here:
http://us.php.net/manual/en/function.fgetc.php
http://us2.php.net/manual/en/wrappers.php.php
There is an example in the first link that I think will help you.
Norm