Solved

Check If Arrow Key Is Pressed

Posted on 1998-03-05
9
727 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 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
 
LVL 3

Expert Comment

by:q2guo
ID: 1183048
ozo, I tried on my computer, it worked out.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 …
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 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.

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now