If i'm scanning from keyboard using getch(), how do i check which arrow key has the user pressed??(Arrow keys have ASCII values same as some other keys so we can't directly check for ASCII values)

That would depend on your operating system and compiler.

So which do you have?
Try to be more specific than "windows" and "borland", as there are many versions of each.

That would depend on your operating system and compiler.

So which do you have?
Try to be more specific than "windows" and "borland", as there are many versions of each.

I'm using DOS and the compiler is turboc..

Try this simplest program, look at these codes on your screen:

#include <stdio.h"
#include <conio.h>
/* Press Esc to quit */
#define ESC 27
int main()
{
    int ch;
    while ((ch=getch()) != ESC)
    {
        printf("%d",ch);
        while (kbhit())
        {
            printf(" %d",getch());
        }
        printf("\n");
    }
    printf("ESC %d\n",ch);
    return 0;
}

Special keys are presented by two codes, as usually the 1st is 0 (zero).
So you need two getch call to accept these keys.
Take a pen and a piece of paper, start the program then press your favorite keys...

This program works not only with unfading Turbo C...

Good luck!

I'm using DOS and the compiler is turboc..

This program only works in Turbo C.

#include <stdio.h>
#include <conio.h>

int main(void)
{
  unsigned char key, key2;
  
  while( (key=getch()) != '\r' )
  {
    if (key == 0)
    {
      key2 = getch();
      switch(key2)
      {
        case 72:
          printf("up "); break;
        case 75:
          printf("left "); break;
        case 77:
          printf("right "); break;
        case 80:
          printf("down "); break;
        default: break;
      }
    }
  }
  return 0;
}

Actually, if you need to simply accept input, you don't need kbhit() at all:

c2 = 0;            // reset c2 to 'nothing'
c1 = getch();      // wait for a key
if (c1 == 0)       // check if it's a function key
{
    c2 = getch();  // it is, get the function value
}
//  if c2 is not 0, a function key was pressed

kbhit() is useful when you want the program to continuously run and when a key is pressed do something different. For example:

if (kbhit())        // check to see if a key was pressed
{                   // a key was pressed so we interrupt the standard 
                    //     programming cycle
    c2 = 0;         // reset c2 to 'nothing'
    c1 = getch();
    if (c1 == 0)
    {
        c2 = getch();
    }
    //  if c2 is not 0, a function key was pressed
    ...
}

About kbhit(): look at my program example then try to print the second byte of a special key without kbhit() at the same line...

About kbhit(): look at my program example ...

I did, and don't see why you need a kbhit() for that. A simple test for 0 would work.

... then try to print the second byte of a special key without kbhit() at the same line...

Simple without the kbhit() . Look at my code and make the simple change...

Alas, not all special keys have 0 as a 1st byte of getch() pair (F11, F12 on Windows XP in console applications, for example)...

Yeah, I know. The concept is still correct. As a matter of fact, the first byte for those keys has been 0xE0 long before XP was created.

Will we really dispute about concepts of so fundamental issue as getch, kbhit and other conio stuff using?..

In C++, you simply need to check the _getch() twice.
using an integer variable, the arrow keys will return the integer "224"
when arrow keys are pressed. Since arrow keys have 2 bytes or bit (not so sure) of values, or so I've read, you will need to check for the second byte/bit to identify which direction was pressed. Here is a sample code.

#include <iostream>
#include <conio.h>

using namespace std;

main()
{
      system("cls");
      int x;
      
      x=_getch();
      if(x==224)
      {
       x=_getch();
       switch(x)
       {
        case 72:
             cout << "Up ";
             break;
        case 75:
             cout << "Left ";
             break;
        case 77:
             cout << "Right ";
             break;
        case 80:
             cout << "Down ";
       }
       cout << "was pressed!";
      }
      
      cout << endl << endl;
      system("pause");
      return main();
}
commented: resurrecting for no new information -4

And what new information has been added by resurrecting a 3-year old thread? I see nothing that hasn't already been mentioned.

For this problem you refer the book let us c......
I think the chapter number is 8 or 9 is about Keyboard

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.