getchar() with the arrows
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > getchar() with the arrows




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    getchar() with the arrows  
Calle


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

My code look like this:

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
int key;
key = getchar();

cout << key << endl;

if (key == 333)
cout << "You pressed the rightarrow key!" << endl;
}

The problem is that if I press on of the arrow keys, the only thing I
get is 27, which is ESC.

Is it possible to use the arrow keys, and if it is, how do I use them?





[ Post a follow-up to this message ]



    Re: getchar() with the arrows  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

calle8787@hotmail.com (Calle) writes:

> My code look like this:
>
> #include <iostream>
> #include <stdio.h>
>
> using namespace std;
>
> int main()
> {
>    int key;
>    key = getchar();
>
>    cout << key << endl;
>
>    if (key == 333)
>       cout << "You pressed the rightarrow key!" << endl;
> }
>
> The problem is that if I press on of the arrow keys, the only thing I
> get is 27, which is ESC.
>
> Is it possible to use the arrow keys, and if it is, how do I use them?

Do you use a function named getkey? No. You're using a function getchar.
So you're getting a character, not a key!

Some of the problems with keys are that:

- each terminal has it's own codes for keys,

- they're not useful in batch processing (so a unix program written
to behave interactively with some terminal key can't work from
scripts, check for example all these people asking for scripting
ftp(1)! (And it does not even use "keys"!).

- etc.

But if you really need to, either you could use curses (or ncurses)
where there are functions to handle keys.

--
__Pascal Bourguignon__                     http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein





[ Post a follow-up to this message ]



    Re: getchar() with the arrows  
Mark A. Odell


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

calle8787@hotmail.com (Calle) wrote in
news:f81f4cdc.0406231229.1688957f@posting.google.com:

> My code look like this:
>
> #include <iostream>
> #include <stdio.h>
>
> using namespace std;
>
> int main()
> {
>    int key;
>    key = getchar();
>
>    cout << key << endl;
>
>    if (key == 333)
>       cout << "You pressed the rightarrow key!" << endl;
> }
>
> The problem is that if I press on of the arrow keys, the only thing I
> get is 27, which is ESC.
>
> Is it possible to use the arrow keys, and if it is, how do I use them?

Why do you not return an int from main() as you are supposed to?

Since the arrow keys are two characters you will need to use the ESC char
to enter a "looking for next char" mode to determine which  key was
pressed. But of course this won't work with getchar() since it only
returns after a newline is detected. You should be looking for some system
specific method for looking at keyboard key hits as they occur instead of
a system-independent language feature such as getchar(). I've seen it done
with select() and read().

--
- Mark ->
--





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:38 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register