1
#include <conio.h>
#include <iostream>

#pragma warning(disable : 4996)

int main()
{
    while (true)
        std::cout << "-----\n" << getch() << "\n-----\n";
}

That's what I get If I press the space key:

-----
32
-----

If I press the del key for example I get this:

-----
224
-----
-----
83
-----

Why do some keys print one number and some other keys print two numbers?

ljrk
  • 751
  • 1
  • 5
  • 21
StackExchange123
  • 1,871
  • 9
  • 24
  • 2
    That is the way it is designed. Function and cursor keys do not provide the ASCII values of typing keys. Windows can return 2 keys, on Linux it can be 3, 4 or 5 (including the leading `ESC` which must be somehow distinguished from from the `Esc` key). – Weather Vane Apr 18 '20 at 20:01
  • 3
    To be able to represent all possible keys on all possible keyboards, not all keys can be represented by a single byte. That means some will have to return two (or more) bytes. This is rather well-documented and should be easy to find with your favorite search engine (or in [the `getch` documentation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019)). – Some programmer dude Apr 18 '20 at 20:01
  • 1
    The 224 is an indicator that it is an [extended key code](https://stackoverflow.com/a/54581468/5231607). – 1201ProgramAlarm Apr 18 '20 at 20:02
  • delete does have a value in ASCII however (127), wonder why that wasn't used. – john Apr 18 '20 at 20:02
  • @1201ProgramAlarm for some (Windows). Others have `0` as the first value. – Weather Vane Apr 18 '20 at 20:04
  • Delete and Backspace have their own long story, beginning in the days of physical TeleTYpes (not to be confused with videoterminals) – numzero Apr 18 '20 at 20:17
  • Please do not tag C++ questions as C, those are different languages. – ljrk Apr 19 '20 at 09:02

0 Answers0