Solved

Check If Arrow Key Is Pressed

Posted on 1998-03-05
9
725 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 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

23 Experts available now in Live!

Get 1:1 Help Now