13

I am sure this must have been asked before, but a quick search found nothing.

How can I get the arrow/direction keys with cin in c++?

Baruch
  • 20,590
  • 28
  • 126
  • 201

2 Answers2

15

It has indeed been asked before, and the answer is that you cannot do it.

C++ has no concept of a keyboard or a console. It only knows of an opaque input data stream.

Your physical console preprocesses and buffers your keyboard activity and only sends cooked data to the program, usually line-by-line. In order to talk to the keyboard directly, you require a platform-specific terminal handling library.

On Linux, this is usually done with the ncurses or termcap/terminfo libraries. On Windows you can use pdcurses, or perhaps the Windows API (though I'm not familiar with that aspect).

Graphic-application frameworks such as SDL, Allegro, Irrlicht or Ogre3D come with full keyboard and mouse handling, too.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 9
    You are indeed lucky that you are not familiar with winAPI. Ignorance is bliss! :) – FailedDev Dec 08 '11 at 18:46
  • 1
    @FailedDev: hehe - I know a bit of it, and I've seen my share of Hungarian tongue twisters and `LPHANDLE`s :-) I've just never seen anything related to raw terminal handling... feel free to chime in with some pointers! :-) – Kerrek SB Dec 08 '11 at 18:48
  • :) Haven't done something like this either, although I am sure it's possible. But OP doesn't seem to be interested in specific OS although he should :) – FailedDev Dec 08 '11 at 18:56
  • Don't the direction keys send data to the data stream? Doesn't every keyboard key have a code that that fits in a `char`? I don't understand how this works! – Baruch Dec 08 '11 at 19:04
  • 1
    @baruch: Whether keys map to characters is up to the keyboard driver. Surely shift and control by themselves do not cause characters to be inserted into the input stream. Keyboard scancodes are really a separate layer (e.g. you can distinguish left and right shift by their scancodes, but never by their cooked results). – Kerrek SB Dec 08 '11 at 19:54
12

Here is a pointer if you dont mind using getch() located in conio.h.

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

#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27


int main()
{
   int KB_code=0;

   while(KB_code != KB_ESCAPE )
   { 
     if (kbhit())
      {
            KB_code = getch();
            printf("KB_code = %i \n",KB_code);

            switch (KB_code)
            {
                case KB_LEFT:
                           //Do something
                break;

                case KB_RIGHT:
                           //Do something                     
                break;

                case KB_UP:
                           //Do something                     
                break;

                case KB_DOWN:
                           //Do something                     
                break;

            }        

      }
  }

  return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28