Borland C++ 5.0 input using arrow keys

  • Context: C/C++ 
  • Thread starter Thread starter Ephysics
  • Start date Start date
  • Tags Tags
    C++ Input
Click For Summary

Discussion Overview

The discussion revolves around detecting user input from arrow keys in Borland C++ 5.0, focusing on the technical implementation and challenges associated with handling special key inputs in a programming context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks guidance on how to detect arrow key inputs using the function getch, providing a code snippet as an example.
  • Another participant asserts that getch cannot be used with special keys and suggests looking for OS-specific functions like kbhit or conio, indicating a need for different approaches based on the operating system.
  • A later reply clarifies that on Windows, special keys generate a sequence of two characters starting with zero, explaining that arrow keys have specific codes that can be distinguished from normal ASCII characters.
  • The provided example program illustrates how to capture and display these codes, including the specific values for each arrow key.

Areas of Agreement / Disagreement

Participants express differing views on the use of getch for detecting special keys, with some asserting it is not suitable while others provide alternative methods. The discussion remains unresolved regarding the best approach to implement arrow key detection.

Contextual Notes

There are limitations regarding the assumptions about the operating system and the specific libraries available for Borland C++ 5.0, which may affect the applicability of the proposed solutions.

Ephysics
Messages
3
Reaction score
0
hi!
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
Ephysics

PS-
Is this correct ?
int c = getch();
if(c == 0)
{
c = getch();
c*=256;
}
 
Technology news on Phys.org
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
 
mgb_phys said:
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

I am sorry, I don't understand what you are saying.
Ephysics
 
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:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int argc, char *argv[]){
    unsigned car=' ';
    do{
        if(car==0)car=getch()+256;
        printf("%ud",car);
    }while((car=getch())!='\033');
    return 0;
}
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
15
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 3 ·
Replies
3
Views
6K