How to read arrow key input in the ANSI way?

P

pete

Jordan said:
If you are willing to weaken down to POSIX.1 instead of pure portable
C, then POSIX.1 has appropriate mechanisms.

could u tell me the way POSIX.1 handling keyboard event?thanks a lot

It would be better to ask in a unix or posix programming newsgroup.

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()

That's hardly enough - to get "arrow keys" you also need curses

Damn, damn!!!
 
A

Alan Balmer

If you are willing to weaken down to POSIX.1 instead of pure portable
C, then POSIX.1 has appropriate mechanisms.

could u tell me the way POSIX.1 handling keyboard event?thanks a lot

It would be better to ask in a unix or posix programming newsgroup.

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()

That's hardly enough - to get "arrow keys" you also need curses

An excellent illustration of why off-topic questions should not be
answered here ;-)
 
W

Walter Roberson

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()
That's hardly enough - to get "arrow keys" you also need curses

Not really.

termcap or terminfo give you convenient ways of representing which
character sequence means which arrow key. If you have turned off echo
and all you need is to know which arrow key has been pressed, then
terminfo / termcap or any other such correspondance table would do just
as well.

If you want to be able to position the cursor to a particular point
on the screen, -then- you bring in curses rather than reinventing the
wheel. Provided, that is, that you are working with a text-based
output rather than a graphical output.... but of course if you
are working with graphical output, you are rather distant from
the original goal of using pure C to deal with arrow keys.
 
K

Keith Thompson

[...]
___________________
/| /| | |
||__|| | Please do |
/ O O\__ NOT |
/ \ feed the |
/ \ \ trolls |
/ _ \ \ ______________|
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ \ __||
/ / \ |____| ||
/ | | /| | --|
| | |// |____ --|
* _ | |_|_|_| | \-/
*-- _--\ _ \ // |
/ _ \\ _ // | /
* / \_ /- | - | |
* ___ c_c_c_C/ \C_c_c_c____________
 
J

Jordan Abel

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()
That's hardly enough - to get "arrow keys" you also need curses

Not really.

termcap or terminfo give you convenient ways of representing which
character sequence means which arrow key. If you have turned off echo
and all you need is to know which arrow key has been pressed, then
terminfo / termcap or any other such correspondance table would do just
as well.

terminfo is part of curses, isn't it?

Regardless, this is offtopic.
 
M

Mabden

Oh please.

Keith Thompson said:
[...]
___________________
/| /| | |
||__|| | Please do |
/ O O\__ NOT |
/ \ feed the |
/ \ \ trolls |
/ _ \ \ ______________|
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ \ __||
/ / \ |____| ||
/ | | /| | --|
| | |// |____ --|
* _ | |_|_|_| | \-/
*-- _--\ _ \ // |
/ _ \\ _ // | /
* / \_ /- | - | |
* ___ c_c_c_C/ \C_c_c_c____________
 
M

Mabden

Walter Roberson said:
?? In previous threads you have indicates that you are Canadian.
http://groups.google.ca/group/comp....e448484befc/de4d53d4d7d82900#de4d53d4d7d82900

Yes, but I am doing contract work in Los Angeles. I just pointed that
out because I was talking about how great America was to the over-seas
guys, but that I am not actually an American saying it. Sort of like
full disclosure. "As a foreigner who likes what America has produced..."
Unfortunately, other than beaver fur hats, we don't have a lot of
inventions, eh?!

BTW, that is another reason I don't mind criticizing "foreign"
ationals - I am one. If you are going to steal American jobs do it
right!

BTW2, I refuse to talk to Indian telephone support, as well. I try to
get American help desk people who are in America, and I ask about what
city they are in, etc, and people not in prison. I don't talk to either
group when I need help with my credit card or bank account. You don't
like it; tough shit.
 
S

Skarmander

Mabden said:
> news:[email protected]... [troll food warning]
>
Oh please.
No, he's quite right. Ranting about various issues with foreign
nationals is demonstrably unproductive and off-topic, the classic
hallmarks of a troll post. You don't have to be a full time troll to troll.

Whether you're doing it intentionally to stir up the flames or just
because you like treating the ng like a public forum is not as relevant
as the fact that it's a waste of collective time.

At least have the decency to mark flamage off-topic, so we can consider
ourselves fairly warned. Better yet, just don't post it.

S.
 
M

Mabden

Paramesh said:
Mabden,
First, i appologize for my post.I'm not sure why i got all mad, its not
like me.

Thank you for that. I hope my words weren't taken too hard by you
either.
Remember one thing Mabden.
No one is born knowing everything.

Many people on this newsgroup will tell you all about the fact that I
don't know everything ("anything" will be their word, probably)
Yes. True. I accept that.

No. thats not true. You are not god my friend, to tell how others will
perform.
How do you think like that?

I meant it more as a "right now", not as "ever"

You were also once new to C isnt it?
So, you say it depends on the country where we live? no.
Think about that.

Not at all. I don't think that people from different countries can't
learn. I agree you can one day be a fine programmer. It takes time and
effort.
Not at all.
Once again, You were also new to programming once.

I just see jobs going to people who are new to programming, such as
yourself, instead of professional programmers who have been doing it
since there were computers to program, in a cost-cutting effort that is
short-sighted in that the code will not be a thouroughly thought out and
may not be as professional, just because you haven't done it as long;
not because I think you can't be good at it.
I am still learning C. think about the time when you were learning C.
You also didnt know at the start how to do this isnt it?
So, dont think everyone has the same knowledge as you, and all the
programs are very simple.

Why?

OK, that was insulting to you, and it's my turn to apologize. Sorry.
Lets see what happens in the next 10 years.

Indeed. Perhaps we'll have a beer (or a curry, if you don't drink) and
laugh about how we met in this newsgroup! :)
 
A

Abhi

You can use getch() to read the arrow keys.
But unlike normal character, for arrow keys you will receive two
keystrokes. First one is special stroke, while from second you can
identify, which arrow key is pressed.
For example 75 for up, 76 for down, etc.

-Abhishikt
 
M

Mike Wahler

Abhi said:
You can use getch() to read the arrow keys.
But unlike normal character, for arrow keys you will receive two
keystrokes. First one is special stroke, while from second you can
identify, which arrow key is pressed.
For example 75 for up, 76 for down, etc.

Please don't post such answers in comp.lang.c

Your solution above could indeed be the 'best' for
some platforms, but does not exist at all in ISO
standard C, the only topic here.

-Mike
 
K

Keith Thompson

Abhi said:
You can use getch() to read the arrow keys.
But unlike normal character, for arrow keys you will receive two
keystrokes. First one is special stroke, while from second you can
identify, which arrow key is pressed.
For example 75 for up, 76 for down, etc.

Please don't top-post. Your response goes below any quoted text so
the entire article can be read from top to bottom.

As several people have already mentioned in this thread, there is no
getch() function in standard C. There a several incompatible
functions by that name on various systems. We have no way of knowing
which one you're talking about; even if we did, it would be off-topic
in this newsgroup.

Since there happens to be no portable way to do what Paramesh is
asking for, any discussion of specific solutions belongs in newsgroups
appropriate to whatever system is being used.
 
M

Mabden

Skarmander said:
Mabden said:
news:[email protected]... [troll food warning]

Oh please.
No, he's quite right. Ranting about various issues with foreign
nationals is demonstrably unproductive and off-topic, the classic
hallmarks of a troll post. You don't have to be a full time troll to
troll.

Well, I at least directed him to look to another newsgroup about his OS,
which no one else has done.
Whether you're doing it intentionally to stir up the flames or just
because you like treating the ng like a public forum is not as relevant
as the fact that it's a waste of collective time.

Yeah, guilty I guess, on the irrelevant public forum part, not the
intentional stirring part.
At least have the decency to mark flamage off-topic, so we can consider
ourselves fairly warned. Better yet, just don't post it.

That advice and a time machine would go a long way to change a lot of my
posts. Sometimes I'm just grumpy. Sometimes I'm drunk and grumpy! :-O
 
S

Skarmander

Mabden said:
That advice and a time machine would go a long way to change a lot of
my posts. Sometimes I'm just grumpy. Sometimes I'm drunk and grumpy!
:-O

Oh, that's the classic mistake, that is. Don't post to Usenet while
drunk... I'm sure it's covered in the various netiquettes somewhere.

S.
 
M

Michael Wojcik

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()
That's hardly enough - to get "arrow keys" you also need curses

Not really.

termcap or terminfo give you convenient ways of representing which
character sequence means which arrow key. If you have turned off echo
and all you need is to know which arrow key has been pressed, then
terminfo / termcap or any other such correspondance table would do just
as well.

terminfo is part of curses, isn't it?

No, it is not; curses is a library built on top of termcap/terminfo.
termcap and terminfo are databases of terminal device capability
information. The original termcap was developed by Bill Joy to make
vi portable to terminals other than the ADM3a, sometime in the late
1970s or early 1980s, and predates the curses library considerably.
The original curses was written by Ken Arnold in 1986; his paper
"Screen Updating and Cursor Movement Optimization" notes right in the
front matter that "[t]hese routines all use the termcap(5) database",
and credits Bill Joy.

There are many programs which use termcap/terminfo directly, and do
not use curses, to detect special keys. I've written a number of
them myself.

As Alan already noted, this is an excellent illustration of why we
shouldn't answer off-topic questions here. I believe everything
above is correct, and I've verified it against various sources, but
it'd be better vetted in, say, comp.unix.programmer.

Suppose the OP saw Walter's post regarding termcap/terminfo, and
your response regarding curses, and read no further, and didn't take
the advice to ask in comp.unix.programmer, but simply decided that
he needed curses. Because curses wants to own the tty, he might
well spend significant time learning curses and rewriting all the
tty I/O in his application to use it, when a little terminfo and
tcsetattr would have done the job. Not a tragedy, but even a smidge
of incorrect information can cost someone some resources they might
have better applied elsewhere.

(I suppose the same can be said about long Usenet posts.)
 
W

Walter Roberson

[OT]
Well, I at least directed him to look to another newsgroup about his OS,
which no one else has done.

- The original message in the thread was posted at
Fri, 04 Nov 2005 07:03:53 GMT. Based upon the OP's IP address, he appears
to be in New Delhi, GMT+5:30, so the posting would have been between
noon and 1 PM for him.

- There was a reply from Mike Wahler at
Fri, 04 Nov 2005 07:10:53 GMT. Mike appears to be in PST, so this
was a bit after 11 PM for him.

- fb posted referring to the FAQ at
Fri, 04 Nov 2005 07:19:46 GMT. fb appears to be in MST, so this was
after midnight for him or her.

- The OP replied to that at
Fri, 04 Nov 2005 07:34:13 GMT (1 PM local) and confirmed that they
wanted a C standards- compliant solution if possible.

- I replied confirming no C standards mechanism was possible, at
Fri, 04 Nov 2005 07:49:55 GMT. I am CST (GMT-6), so that was about
2 AM for me. I went to bed not long thereafter.

- Mike Wahler posted mere minutes later saying much the same, at
Fri, 04 Nov 2005 07:51:15 GMT.

- Two hours later you post mentioning the possibility of other newsgroups,
Fri, 04 Nov 2005 09:49:01 GMT. Your posting appears to be in EST
(about 5 AM local)... though in a later posting you say you are contracting
in LA which would be PST (about 2 AM local)

- Your.... urrr, expressions of personal opinion... began at
Fri, 04 Nov 2005 12:17:54 GMT. This would appear to have been either
about 4 AM or 7 AM for you -- you were either up very late or else
very early.

- Randy Howard referred to other newsgroups at
Fri, 04 Nov 2005 14:19:47 GMT

- I referred to other newsgroups at
Fri, 4 Nov 2005 17:00:51 GMT.

- Keith Thompson referred to other newsgroups at
Sat, 05 Nov 2005 00:45:06 GMT

- You indicated that,
"Well, I at least directed him to look to another newsgroup about his OS,
which no one else has done."
Sat, 05 Nov 2005 10:27:16 GMT

That statement was not correct, as by the time of that posting,
three posters other than yourself had also referred to other newsgroups.

- You -were- the first to to have mentioned other newsgroups, 2 hours
42 minutes after the original posting. At that time you said, "I am
surprised no one has said this yet". Personally, I would not have been
surprised that there were few postings in the middle of the night North
American time.

- If you review the questions asked by the OP, you will see that the OP
already knew about non-standard methods, and was asking about
standard methods. It wasn't a more open "How can I do this in the X, Y,
and Z OSs", it was a specific "Is there a standard C method", so
the three posters before you were answering the question that was asked.

- I think that if you re-examine your posting alluding to other
newsgroups, that you will find that your posting did not clearly
answer the question of whether a standards-based method was available.
(Saying that OS-specific ways are available is not quite the same as
saying that standards-compliant ways are -not- available.)
 
J

Jordan Abel

[OT]
<termios.h>, tcsetattr(), tcgetattr(), c_lflag &~ ICANON,
c_cc[MIN], c_cc[TIME], tcflush()

That's hardly enough - to get "arrow keys" you also need curses

Not really.

termcap or terminfo give you convenient ways of representing which
character sequence means which arrow key. If you have turned off echo
and all you need is to know which arrow key has been pressed, then
terminfo / termcap or any other such correspondance table would do just
as well.

terminfo is part of curses, isn't it?

No, it is not; curses is a library built on top of termcap/terminfo.
termcap and terminfo are databases of terminal device capability
information. The original termcap was developed by Bill Joy to make
vi portable to terminals other than the ADM3a, sometime in the late
1970s or early 1980s, and predates the curses library considerably.
The original curses was written by Ken Arnold in 1986; his paper
"Screen Updating and Cursor Movement Optimization" notes right in the
front matter that "[t]hese routines all use the termcap(5) database",
and credits Bill Joy.

There are many programs which use termcap/terminfo directly, and do
not use curses, to detect special keys. I've written a number of
them myself.

As Alan already noted, this is an excellent illustration of why we
shouldn't answer off-topic questions here. I believe everything
above is correct, and I've verified it against various sources, but
it'd be better vetted in, say, comp.unix.programmer.

Suppose the OP saw Walter's post regarding termcap/terminfo, and
your response regarding curses, and read no further, and didn't take
the advice to ask in comp.unix.programmer, but simply decided that
he needed curses. Because curses wants to own the tty, he might

iirc he said he was already using curses anyway, in which case it hardly
makes sense _not_ to use curses for handling input.

how do you propose to use terminfo without curses to recognize arrow
keys? build your own state machine from it on the fly?
 
W

Walter Roberson

Jordan Abel said:
iirc he said he was already using curses anyway, in which case it hardly
makes sense _not_ to use curses for handling input.

Sorry, you do not recall correctly.

The first indirect reference to curses was fb's citation of the FAQ;
if that link had been chased then the OP would have seen curses mentioned
there.

You were the first to refer to curses directly, saying,which was not technically correct.

Other than the thread that spun off of that, there was no other
references to curses.
how do you propose to use terminfo without curses to recognize arrow
keys? build your own state machine from it on the fly?

Why not? It isn't complex code at all. There are only 4 cursor keys,
so you just keep a list of 4 character arrays (one per key) and a
list of current offsets into that table. New character comes in, run
through the list comparing the new character to the current active
character in each of the four lists. If the character doesn't match,
record the current offset and current entry number if the
current offset is greater than the current recorded offset; then
zero the entry's offset. If it does match, increment the
active offset for that key, and compare the new offset to the
pre-stored length (don't use strlen as \0 might be part of the sequence);
if you are at the end of the entry then you have found your key
so first reset all the offsets to 0 and then mark whatever indicating that
you got a cursor key match; if you were not at the end of the entry
then proceed down to the next list element, which might -also- end
up having its position incremented. After processing the
four entries, if none of the entries moved further along, and
you had a current offset and keynumber recorded from the mismatch
code mentioned earlier, then you had a false match up to there,
so emit that many characters from the appropriate key's character
array.

It takes longer to write as prose than to code in C ;-)
 
G

Gordon Burditt

how do you propose to use terminfo without curses to recognize arrow
Why not? It isn't complex code at all. There are only 4 cursor keys,
so you just keep a list of 4 character arrays (one per key) and a
list of current offsets into that table. New character comes in, run
through the list comparing the new character to the current active
character in each of the four lists. If the character doesn't match,
record the current offset and current entry number if the
current offset is greater than the current recorded offset; then
zero the entry's offset. If it does match, increment the
active offset for that key, and compare the new offset to the
pre-stored length (don't use strlen as \0 might be part of the sequence);
if you are at the end of the entry then you have found your key
so first reset all the offsets to 0 and then mark whatever indicating that
you got a cursor key match; if you were not at the end of the entry
then proceed down to the next list element, which might -also- end
up having its position incremented. After processing the
four entries, if none of the entries moved further along, and
you had a current offset and keynumber recorded from the mismatch
code mentioned earlier, then you had a false match up to there,
so emit that many characters from the appropriate key's character
array.

This approach has problems when one legitimate key sequence (e.g.
the escape key) is a prefix of another legitimate key sequence
(e.g. the arrow keys). About the only way out of this (and it's
a poor one, but it sorta works sometimes, and it's out of the
scope of standard C) is timing.

Gordon L. Burditt
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top