Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Borland C++ 5.0 input using arrow keys

  1. May 29, 2009 #1
    I've started making my own games in Borland C++ 5.0
    I want to learn how to detect the user's input of arrow keys (for eg. when a user presses UP,DOWN ,RIGHT or LEFT)
    Can somebody please post a short tutorial on detecting arrow keys?
    Thank You

    Is this correct ?
    int c = getch();
    if(c == 0)
    c = getch();
  2. jcsd
  3. May 29, 2009 #2


    User Avatar
    Science Advisor
    Homework Helper

    You can't use getch with special keys.
    The lib for your compiler needs to have an OS specific function, it's also different if you are in windows or on a console. Look for something like kbhit or conio
  4. May 30, 2009 #3
    I am sorry, I don't understand what you are saying.
  5. May 30, 2009 #4
    On Windows, the special keys form a chain of two characters, beginning with a zero. It is like another page of ASCII characters with an added value of 256.
    The following example program compiled with Borland C++ should make this clear.

    Compile and run the following program, then you can check the codes required, for example, left/right/down/up arrows have codes 331,333,336,328 respectively with the added 256 to distinguish from the normal ASCII codes. To quit the program, press the escape key (ASCII 27 decimal, octal 33)
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    int main(int argc, char *argv[]){
        unsigned car=' ';
        return 0;
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook