Unix Programming - getchar() with the arrows

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > getchar() with the arrows





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author getchar() with the arrows
Calle

2004-06-26, 10:11 am

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?
Pascal Bourguignon

2004-06-26, 10:11 am

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
Mark A. Odell

2004-06-26, 10:11 am

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 ->
--
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com