Solved

Check If Arrow Key Is Pressed

Posted on 1998-03-05
9
730 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

856 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