Question about detecting key strokes

P

Pratap Das

Hi All,

I am writing sort of a shell in Perl where I am trying to implement
command recall at shell prompt like you have for zsh/tcsh. So when I
press up/down arrow keys I should be able to get the previous/next
command typed.

I saw code in the web to detect arrow keys in which I have to first
put the terminal into 'cbreak' mode for getting each charcter. And the
echo is turned off. Though I am able to detect the key, since the at
least for the first charater the echo is off (I am turning on the echo
if I see that the character is a normal character) so for all the
other shell commands the first character is not echoed to the screen.

Is there a way in which I can detect the key without switching off the
echo so that all other characters can be echoed but at the same time I
don't want to echo the arrow keys when they are pressed.

Any help is appreciated.
Thanks,
--Das
 
J

Joe Smith

Pratap said:
I saw code in the web to detect arrow keys in which I have to first
put the terminal into 'cbreak' mode for getting each charcter. And the
echo is turned off. Though I am able to detect the key, since the at
least for the first charater the echo is off (I am turning on the echo
if I see that the character is a normal character) so for all the
other shell commands the first character is not echoed to the screen.

There's your problem. Arrow keys send escape sequences, and normal
characters are part of that escape sequence. With VT100-compatible
ANSI terminal emulators, the arrow keys send \033[A or \033[B or \033[C
or \033[D (where \033 is the ESCape character, "[" indicates what type
of escape sequence it is, an "A".."D" indicate the end of the escape
sequence).
Is there a way in which I can detect the key without switching off the
echo so that all other characters can be echoed but at the same time I
don't want to echo the arrow keys when they are pressed.

Nope. zsh/tcsh/bash all turn off echo to get a look at the characters.
The code that treats Control-P the same as up-arrow and Control-N the
same as down-arrow is done by the shells.

Anyway, why re-invent the wheel? Just "use Term::ReadLine;".

-Joe

P.S. Don't post here, go to comp.lang.perl.misc to get answers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top