0

I want to know when an arrow key is pressed. For that I've already found _getchar(). Unfortunately, that returns several values. It returns two different and separated values, even if I save it in one integer.

The first value is always 224, and the second one is the one I need. How can I get it, though?

I tried var[1], but that doesn't work, because it's an integer (didn't know one integer can hold several values).

int posX = 5;
int posY = 10;

while(1)
{

    switch(_getch())
    {
        case 72:
            posY++;
        case 80:
            posY--;
        case 75:
            posX--;
        case 77:
            posX++;
    }

    build(posX, posY); // a function to visualize things, not necessary for my problem
}

Thanks :D

Caty
  • 11
  • 1
  • 2
  • Where are you saving something into a variable? – LogicStuff Feb 20 '16 at 15:42
  • Your title says `_getchar()`; your commentary says `_getchar()`; your code says `_getch()` — which do you mean? None of the names is particularly standard — functions in general use on POSIX don't start with underscores, though Microsoft seems to prefer underscores. There's a `getch()` in `` on Windows; there's `getchar()` in `` evrywhere. – Jonathan Leffler Feb 20 '16 at 15:50
  • Just saw that I copied the newer version of my code. I defined an integer c before the switch and added a std::cout too. Then I get those two values – Caty Feb 20 '16 at 15:50
  • 2
    Your question shouldn't be about splitting up the _getch() input but rather detecting if the user has pressed an arrow key. For that, there is already a SO answer for that here: https://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key – Carl Burnett Feb 20 '16 at 15:52
  • Arrow keys often generate multiple bytes. The 72, 75, 77, 80 codes correspond to letters HKMP. On my Mac, the down, right, left, up keys generate the hex sequences `1B 5B 42` (escape `[B`), `1B 5B 43` (escape, `[C`), `1B 5B 44` (escape `[D`) and `1B 5B 41` (escape `[A`) respectively. Your computer probably generates different sequences. That's the way the real world works (different computers do things differently). It also matters which platform you're using, therefore (please add that information to the question). – Jonathan Leffler Feb 20 '16 at 15:55

0 Answers0