PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > How to get a keypress?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to get a keypress?

Réponse
 
LinkBack Outils de la discussion
Vieux 14/09/2007, 23h24   #1
Lars Eighner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to get a keypress?

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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 493 days to go.
What do you do when you're debranded?
  Réponse avec citation
Vieux 15/09/2007, 00h21   #2
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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
  Réponse avec citation
Vieux 15/09/2007, 00h38   #3
Lars Eighner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 493 days to go.
What do you do when you're debranded?
  Réponse avec citation
Vieux 15/09/2007, 00h47   #4
Jim Carlock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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 <head> 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/...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/


  Réponse avec citation
Vieux 15/09/2007, 00h51   #5
Lars Eighner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

In our last episode, <slrnfem6r4.1h1e.usenet@debranded.larseighner.com> , 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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 493 days to go.
What do you do when you're debranded?
  Réponse avec citation
Vieux 15/09/2007, 05h46   #6
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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 <head> 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/...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
  Réponse avec citation
Vieux 15/09/2007, 06h06   #7
Lars Eighner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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 <head> 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/...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

<?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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 493 days to go.
What do you do when you're debranded?
  Réponse avec citation
Vieux 15/09/2007, 12h35   #8
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get a keypress?

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 <head> 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/...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
>
> <?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 you.
Norm
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17656 seconds with 16 queries