How to read arrow key input in the ANSI way?

P

Paramesh

Hello friends,

I want to write a program where i want to use the arrow keys.

I could not find how to read the arrow key input.

The only way i know is the non-ansi way by using getch function. or by
using the conio.h header file.

Is there any other better method?
Please give me a sample code.

Thank you,
Paramesh.
 
M

Mike Wahler

Paramesh said:
Hello friends,

I want to write a program where i want to use the arrow keys.

ISO C has no concept of 'arrow keys', or 'keys',
or 'keyboards'. All i/o is expressed with
'streams of characters'. How a keyboard or
other input device presents this stream to
a C program is platform-specific.
I could not find how to read the arrow key input.

Because it's not there in C.
The only way i know is the non-ansi way by using getch function. or by
using the conio.h header file.

The only way is to use nonstandard methods.
Is there any other better method?
Nope.

Please give me a sample code.

Can't, not in ISO C, the only topic here.

-Mike
 
F

fb

Paramesh said:
Hello friends,

I want to write a program where i want to use the arrow keys.

I could not find how to read the arrow key input.

The only way i know is the non-ansi way by using getch function. or by
using the conio.h header file.

That is because there isn't a way to read specific keypresses.
Check: http://www.eskimo.com/~scs/C-faq/s19.html
Is there any other better method?

Perhaps not better, but there are other ways of getting arrow key-codes.
(But they are not portable)
Please give me a sample code.

Gimme gimme never get. Google it. There are a million examples out there.
Thank you,
Paramesh.

Anytime...
 
P

Paramesh

Thank you for your replys.

Is there any possibility that we could write our own code to get a
keypress event?

i.e can we write a similar getch() function in the ANSI way?

Thank you.
Paramesh.
 
W

Walter Roberson

Is there any possibility that we could write our own code to get a
keypress event?
i.e can we write a similar getch() function in the ANSI way?

Sorry, No.

You could unbuffer your input stream so that you could read
one character at a time from the OS, but the OS or shell
might be buffering the characters until end of line (or end of file)
instead of delivering them upwards immediately. There is no
mechanism in the C standard to ask the OS to deliver the keypresses
individually.

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

Mike Wahler

Paramesh said:
Thank you for your replys.

Is there any possibility that we could write our own code to get a
keypress event?

Yes. But it depends upon your platform and compiler.
Thus, possible methods vary widely.
i.e can we write a similar getch() function in the ANSI way?

Again, NO. ANSI (more correctly, ISO) C has NO
support for 'keys' or 'keyboards'.

-Mike
 
M

Mabden

Paramesh said:
Thank you for your replys.

Is there any possibility that we could write our own code to get a
keypress event?

i.e can we write a similar getch() function in the ANSI way?

I am surprised no one has said this yet, but you might find more help on
a newsgroup about your Operating System or Compiler type. Each OS would
do this a different way, for instance DOS might require a SIGINT or
something (I seem to recall handling the mouse used an int86() call),
whereas Windows would return an event to your program that you can
handle. They are completely different animals.
 
W

winnie

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
 
P

Paramesh

Thank you.

I am using Windows XP operating system and i have three compilers:
mingw
borland 5.5
visual c++ toolkit.

I would be happy if even it works on any one of these compilers.

I wanted to write a console program.

One more doubt:
All say that windows and Linux are written in C.
So, there should be a way to find keypress events isnt it?(but may not
be an ansi way(oh. iso exactly mike).
So, in the non-standard way, which is the best and simple one?
Is it ok to use getch() itself?
and What is POSIX?

Thank you,
Paramesh.
 
W

winnie

So, there should be a way to find keypress events isnt it?
actually there are lots of libs which can do that,svgalib for example.
Is it ok to use getch() itself?
no,i tried,we'd better learn more about curseslib
What is POSIX?
POSIX is a ieee standard about OS interfaces.more information can be
found at ieee.org.
but i still dont know how POSIX handling keyboard events.
 
M

Mabden

Paramesh said:
Thank you.

Stop with the Thank you's. Say what you want, get advice, go away. When
the question is finally answered after a dozen emails, you can throw out
ONE thank you to that person. Don't thank me for asking you what OS you
are using. That is NOT a thank you situation. Stop it.
I am using Windows XP operating system and i have three compilers:
mingw
borland 5.5
visual c++ toolkit.

Get the free .NET framework, and the free compiler. Don't use the old
shit. Move on to the modren world. Use VB if you can't figure out c#.
I wanted to write a console program.

Why. You have the best GUI environment that has ever been created in
this Galaxy, except for the other ones that don't work as well or work
better (I can't tell anymore). Just write a GUI application.
One more doubt:

We say "One more question". You are obviously from India, since I know
of no one else who uses "doubt" instead of "question". I am not a fan of
India, since a lot of jobs that I can do are going to guys like you who
don't know what you are doing (as your "doubt" above indicates - ie, you
need MY help to steal MY job), but I have made peace with that because I
think you will very soon "feel my pain" when China steals your job from
you. As you learn to program computers I hope you realize that in 5 -8
years the job you have will move forever into Beijing and you will once
again have to do whatever non-technical job your father did.
All say that windows and Linux are written in C.

Most things are. Some assembler when necessary. Higher level languages
when performance isn't an issue.
So, there should be a way to find keypress events isnt it?

"isn't it" should be "shouldn't there"

There are many, but trying to do so in a console box under Windows begs
the question of why you would want to do that? Windows will send you
events for every keystroke, so why open a console box to try to read
keyboard scancodes. Just stay in Windows. Learn to write programs in
Windows. Why learn something from 30 years ago?
 
W

winnie

Mabden,i have to say you are such a loser,ha!
Complaints will do nothing good to ur job thing,come on and summon up!
 
P

Paramesh

Hey Mabden,
Your post indicates that you dont want to help others.
-----------------------------------------------------------
We say "One more question". You are obviously from India, since I know
of no one else who uses "doubt" instead of "question". I am not a fan
of
India, since a lot of jobs that I can do are going to guys like you who

don't know what you are doing (as your "doubt" above indicates - ie,
you
need MY help to steal MY job), but I have made peace with that because
I
think you will very soon "feel my pain" when China steals your job from
you. As you learn to program computers I hope you realize that in 5 -8
years the job you have will move forever into Beijing and you will once
again have to do whatever non-technical job your father did.
----------------------------------------------------------

Was your comment really necessary? I take criticism well, but not
cheap shots. But since i dont want this thread to turn into a flaming
thread i hope that this will be the end of conversation with you.

How can you say that " since a lot of jobs that I can do are going to
guys like you who
don't know what you are doing"
How do you know that?
Stop posting such things.
If you know C, shut up and sit quiet.

I am still a student. I wanted some help. If you cant help, stop
posting instead of criticizing. OK?

We Indians are really clever people. If you are so clever, it is up to
you to find a job.
If China has equal opportunities, it is well and good. I really
appreciate that.
I will never criticize other country or i dont have any ill feelings to
any country.

I feel extremely sorry for this post and this issue. But this is the
only way to stop Mabden and his criticisms.

I wanted to say thank you for your help. But from your post, i wont say
that.

Ok. lets leave this issue aside and concentrate on our good work.

Paramesh.
 
R

Randy Howard

Paramesh wrote
(in article
Thank you for your replys.

Is there any possibility that we could write our own code to get a
keypress event?

Yes. How is platform-dependent, and to be discussed in a
newsgroup that caters to your platform, whatever it may be.
i.e can we write a similar getch() function in the ANSI way?

"the ANSI way"? Unlikely. You might try doing it in "the ISO
way", but only if you intend to push /very hard/ for it to be
adopted in the next ISO C standard. Given that it can't be done
on a huge number of platforms that have no arrow keys at all, I
don't think you'll be very successful in that endeavor.
 
M

Mabden

Paramesh said:
Hey Mabden,
Your post indicates that you dont want to help others.

I gave you help. You are inconsiderate to ignore that and insult me.
-----------------------------------------------------------
We say "One more question". You are obviously from India, since I know
of no one else who uses "doubt" instead of "question". I am not a fan
of
India, since a lot of jobs that I can do are going to guys like you who

don't know what you are doing (as your "doubt" above indicates - ie,
you
need MY help to steal MY job), but I have made peace with that because
I
think you will very soon "feel my pain" when China steals your job from
you. As you learn to program computers I hope you realize that in 5 -8
years the job you have will move forever into Beijing and you will once
again have to do whatever non-technical job your father did.

I don't think it is a "cheap shot" to say I'm not a fan of India. I'm
not. I don't like that jobs from the USA are going there. The fact is
that they are, and the fact is I don't like it. I didn't curse the
country, I just said I don't approve. How is that a "cheap shot"? It is
my opinion.

My opinion is that I know C better than you. I think you couldn't
program C to USA standards at all. But I also feel that you might be
hired for a job I could do easily, because you will take less money. I
feel (in my opinion) that I would do a better job for my employer,
because I understand the full life-cycle of code - it needs to be
changable and easily understood. Someone new to programming, as you are,
may get the work because you will work cheap - and you may cheapen
programming in general and make it harder for everyone who comes after
you.
But since i dont want this thread to turn into a flaming
thread i hope that this will be the end of conversation with you.

Nice try, but you can't post so much crap about someone and say "this
should end it". If you want to end the conversation, just don't post.
How can you say that " since a lot of jobs that I can do are going to
guys like you who
don't know what you are doing"
How do you know that?

You are asking my help, that's how I know that. You can't figure out how
to write the simplest of Windows programs in an environment where the OS
HANDS you the keystrokes in events... Come on, who do you think you aref
ooling here. Anyways, I gave you the only good advice you got; you
should just get over to a Windows newsgroup. And you really shouldn't
even come back here, because you don't write C code and never will. My
advice was perfect FOR YOU.
Stop posting such things.
If you know C, shut up and sit quiet.

I am still a student. I wanted some help. If you cant help, stop
posting instead of criticizing. OK?

First: don't ever tell me what to do. I will not do it - to extremity.
Really. You have no control over other people and "telling" them is
never going to work. So now I have schooled you again, you need to start
sending me tuition money... ;-)
We Indians are really clever people. If you are so clever, it is up to
you to find a job.
If China has equal opportunities, it is well and good. I really
appreciate that.
I will never criticize other country or i dont have any ill feelings to
any country.

For a country that was under English rule for so long, you have a real
lack of knowledge about speaking English. That aside, I did not
criticize your country, I said I was not a "fanatic supporter" of it,
that is what "fan" is short for. Your words: "If you are so clever, it
is up to you to find a job." will be echoing in your ears in less than
10 years, my friend. Count on it.
I feel extremely sorry for this post and this issue. But this is the
only way to stop Mabden and his criticisms.

You cannot stop the Mabden horde, my friend. The are the new race,
created by Chaos to bring down the Elder Races. The horde cometh.
I wanted to say thank you for your help.

You are welcome.
 
P

Paramesh

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

Remember one thing Mabden.
No one is born knowing everything.
My opinion is that I know C better than you.
Yes. True. I accept that.
I think you couldn't program C to USA standards at all.
No. thats not true. You are not god my friend, to tell how others will
perform.
How do you think like that?
You were also once new to C isnt it?
So, you say it depends on the country where we live? no.
Think about that.
Someone new to programming, as you are,
may get the work because you will work cheap - and you may cheapen
programming in general and make it harder for everyone who comes after
you.

Not at all.
Once again, You were also new to programming once.
You are asking my help, that's how I know that. You can't figure out how
to write the simplest of Windows programs in an environment where the OS
HANDS you the keystrokes in events... Come on, who do you think you aref
ooling here.
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.
Anyways, I gave you the only good advice you got; you
should just get over to a Windows newsgroup. And you really shouldn't
even come back here, because you don't write C code and never will. Why?


First: don't ever tell me what to do. I will not do it - to extremity.
Really. You have no control over other people and "telling" them is
never going to work.
I agree and apologize.
For a country that was under English rule for so long, you have a real
lack of knowledge about speaking English.
For those who have english as their mother tongue, they will speak
english well.
Speaking english is no problem. It is just a matter of practice.
Your words: "If you are so clever, it
is up to you to find a job." will be echoing in your ears in less than
10 years, my friend. Count on it.
Lets see what happens in the next 10 years.

Regards,
Paramesh.
 
W

Walter Roberson

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()
 
J

Jordan Abel

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top