Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check If Arrow Key Is Pressed

Posted on 1998-03-05
9
Medium Priority
?
739 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
  • 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 100 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 85

Expert Comment

by:ozo
ID: 1183047
No, getch() is actually an int
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 85

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

972 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