954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to detect arrow keys??

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)

mail2shrid
Newbie Poster
2 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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..

mail2shrid
Newbie Poster
2 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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!

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
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;
}
Colin Mac
Posting Whiz
327 posts since Sep 2006
Reputation Points: 78
Solved Threads: 22
 

Look at this wonderful table:
http://www.jimprice.com/jim-asc.shtml#keycodes
Google is your friend ;)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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
    ...
}
WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
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 atmy code and make the simple change...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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();
}

Takari1994
Newbie Poster
1 post since Sep 2011
Reputation Points: 6
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

ashok1514
Newbie Poster
17 posts since Aug 2011
Reputation Points: 6
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You