Scan Codes

I wanted to use arrows in my console program, so I found on the net about those scan codes and I was able to use arrows with this '\0H' for up arrow and so on, but the problem is that it works also when the user press H, so I want it to work only with arrows. Any idea would be greatly appreciated.
'\0H' is a multicharacter constant -- and never a good idea to use.

For a list of scancodes, see:
http://www.jimprice.com/jim-asc.shtml#keycodes

This is only useful, really, on old DOS systems using <conio.h> or the BIOS input routines directly. (Or if you write your own keyboard interrupt handler.)

If you are using Windows you are better off using the Microsoft Console Functions to read keys.
http://www.google.com/search?btnI=1&q=msdn+Console+Functions

Hope this helps.
OK, i will use the Microsoft Console Functions to read keys instead of _getch();
And yeah I already read all the scancodes, but I still need something to differ them(H and up arrow).
Last edited on
Sorry, I wasn't clear enough.

Extended scan codes, like the up arrow, come in two codes, not one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (_getch())  /* read the first key code */
  {
  case 0:  /* introduces an extended key */
  case 227:  /* this also happens on Win32 (I think) */
    switch (_getch())  /* read the extended key code */
      {
      case 72: /* up arrow press */ break;
      case 75: /* left arrow press */ break;
      case 77: /* right arrow press */ break;
      case 80: /* down arrow press */ break;
      /* etc */
      }
    break;

  case 'H': /* capital 'H' key press */ break;
  /* etc */
  }

Windows keyboards have the 227 (I think -- I'm not going to look up the actual number) key code -- some <conio.h> implementations return that instead of zero.

In any case, that's it. If you use the Windows Console Functions (ReadConsoleInput) you'll get the virtual key code, which is much more convenient:
http://www.google.com/search?btnI=1&q=msdn+Virtual+Key+Codes

Good luck!
No, you made yourself clear and I knew about two codes, but I was missing this _getch() twice call and case 0, but this code is all I need, I will try it right away, thx
:D, the code you provided does not work, put cout<<"something"; next to case and it will not show up when I hit arrow. And I am a total beginner, how do you use ReadConsoleInput, I am confused with this msdn library ?
Please use your brain. [edit]Er, sorry to have been so grouchy. I realize you are an absolute beginner -- sometimes it is easy to forget what that is like...[/edit]

I did mention that I wasn't sure about that 227 -- the correct number is 224, or 0xE0 -- the 'WindowsTM Keyboard Extended Key Prefix Code'. I figured it out with a simple cout statement to print the values obtained from getch():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <conio.h>
using namespace std;

int main()
  {
  int c;
  cout << "Press Esc to quit.\n";
  do
    {
    c = getch();
    cout << c << endl;
    }
  while (c != 27);
  return 0;
  }

You will notice that the arrow keys return 224 as a prefix, while the numeric keypad will return 0 as a prefix (when NumLock is off). So, to read the arrow keys, use the code I gave you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int c;
switch (c = getch())
  {
  case 0:
  case 224:
    switch (c = getch())
      {
      case 72: cout << "up arrow\n"; break;
      case 75: cout << "left arrow\n"; break;
      case 77: cout << "right arrow\n"; break;
      case 80: cout << "down arrow\n"; break;
      default: cout << "extended key " << c << "\n";
      }
    break;
  default: cout << "normal key " << c << "\n";
  }

If you really want to play with ReadConsoleInput() I can help you... but you should know that it is a bit more involved than using a simple getch().

Time to hop young grasshopper.
Last edited on
I guess you always wanted to say that (grasshopper) to someone :D But that's fine cuz I am not insulted (how should I when I really suck) . Now to get serious, it works fine and thx for your time and nerves, I'll do more research about this (Y).
Topic archived. No new replies allowed.