Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

keyboard values

Status
Not open for further replies.

Denster

Programmer
Apr 9, 2001
198
GB
I am writing a program that uses the arrow keys on the keyboard. can somebody tell me the best way to go about this or tell me the values for these keys.
thanks

Denster
 
Here is a list I have. It is a bit old but I don't think it has changed. Assuming that:
Code:
int c;

Then if
Code:
c = getch();
and the user presses an arrow key, c should return
72 = Arrow up
75 = Arrow left
77 = Arrow right
80 = Arrow down

If you want to see what other keys return, try this:
Code:
#include <conio.h>
#include <iostream.h>

int main()
{
    char c, k; // characters for key presses

   while (1)
   {
        // loop forever
        c = getch(); // get a key press
        if (!c)
        {
             // Some keys generate two bytes, the first is a NULL
             k = getch(); //  So we need to get more
             cout << c << &quot;, &quot; << k << endl; // Show what we have, including NULL
         }
         else
         {
             // Otherwise we can just print the key code
             cout << c << endl;
          }
     if (c=='q') break; // Lets get out of here
}

This is some code I picked up off a newgroup several years ago. I haven't tested it on some of the newer compilers so beware.

James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top