Solved

Check If Arrow Key Is Pressed

Posted on 1998-03-05
9
736 Views
Last Modified: 2012-06-21
How to detect that someone has pressed up, down, left, or right arrow key ?

I  used  getch(); but the integer that I got is 244 for all those 4 keys.
I also tried calling getch(); twice because someone told me that when I press
special keys, integer 0 will be returned first, then the 2nd getch(); will return
the true value. This doesn't work either.

Is getch() standard for C++? In which library getch() is? conio.h or stdio.h ?

Someone gave me this code but it didn't work.

  int ch = getch();
  if(ch == 0) {
      ch = getch();      // retrieve the second byte
        switch (ch) {
            case UP_ARROW:
                // do some code
                break;
            case DOWN_ARROW:
                // do some code;
                break;
            case LEFT_ARROW:
                // do some code;
                break;
            case F1:
                // do some code;
                break;
            case F2:
                // do some code;
                break;
             // ... etc ...
        }                        
    }
0
Comment
Question by:bubblebobble
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1183045
What platform are you developing on?
0
 
LVL 3

Accepted Solution

by:
q2guo earned 50 total points
ID: 1183046
the variable ch should be delcared as a char
so, line
int ch = getch();
should read
char ch = getch();

Also, remember to include the header file conio.h.
by the way, what did you set constant LEFT_ARROW
UP_ARROW, RIGHT_ARROW, DOWN_ARROW.  They should equal to
75 72 77 80 respectively
0
 
LVL 84

Expert Comment

by:ozo
ID: 1183047
No, getch() is actually an int
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:q2guo
ID: 1183048
ozo, I tried on my computer, it worked out.
0
 

Author Comment

by:bubblebobble
ID: 1183049
q2guo, I haven't changed ch type yet, but getch() should return int. Here is what MSVC help said

Get a character from the console without echo (_getch) or with echo (_getche).
int _getch( void );
int _getche( void );

The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

Also, what is the different between getch() and getchar() ?? Which one is standard?
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183050
Are you using Visual C++??
0
 

Author Comment

by:bubblebobble
ID: 1183051
yes and also I tested my code using gnu c++
0
 
LVL 1

Expert Comment

by:vsinha
ID: 1183052
There are three functions normally used to get char. from the console: getc, getch and getchar.
getchar() is equivalent to getc(stdin)
getc(stream) reads a character from a stream
getch() is different.

getch() is used [together with ungetch()] when it is necessary to 'put back' characters into the stream. Thse two functions work by using a common buffer for the characters.

you could try using getchar() instead of getch().
0
 
LVL 84

Expert Comment

by:ozo
ID: 1183053
q2guo, I didn't mean that char won't work, I meant that the int was not a problem.
getchar and is part of the ANSI C standard, and so should be portable, while getch is not.
On the other hand, getchar makes no particular guarantees about buffering or echoing.
THe ANSI standard function to put back a getc(FILE *stream) is ungetc(int c,FILE *stream)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question