C/C++ Borland C++ 5.0 input using arrow keys

  • Thread starter Thread starter Ephysics
  • Start date Start date
  • Tags Tags
    C++ Input
AI Thread Summary
The discussion focuses on detecting user input for arrow keys in Borland C++ 5.0. A user seeks guidance on how to implement this functionality. It is clarified that the `getch()` function cannot directly handle special keys like arrow keys, as they generate a two-character sequence starting with a zero. To properly detect arrow key inputs, an OS-specific function is necessary, and alternatives like `kbhit` or `conio` may be utilized. A sample program is provided, demonstrating how to capture and display the codes for the arrow keys, which are modified by adding 256 to distinguish them from standard ASCII characters. The program also includes a method to exit using the escape key.
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;
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top