Kevin Dong
Kevin Dong

Reputation: 5369

What does getch() really get? Scan codes?

#include <conio.h>

int main (void)
{
    for (;;)
    {
        unsigned char ch = getch();
        printf ("0x%02X\n", ch);
    }
}

I want to get the scan code.

Here is the description from Wikipedia:

    Reads a character directly from the console without buffer, and without echo.

When I pressed Del, it shows 0xE0 0x53.

When I pressed Ctrl+PgUp, it shows 0xE0 0x86.

Though some are the same as the table, most of the values it shows are different from it.

So, does getch() really get scan codes?

Here is the scan code table (set 2, most commonly used)

Scan Code

Upvotes: 6

Views: 2272

Answers (1)

Nerf Herder
Nerf Herder

Reputation: 414

I ran a console app in VS 2012 (C++) and got the same results as you. It did say that getch() was deprecated and I should use _getch(), but that made no difference.

I then found a table here that matches it (right-most column). For me, the "white" keys were using shift and the numeric keypad equivalents, the "grey" keys were the stand-alone page-up, page-down, etc. The one discrepancy I found was enter on the numeric keypad is the same as the regular enter key.

http://www.itlnet.net/programming/program/Reference/msc/ng7d68f.html

I don't know where this table comes from, other than if you click on "About The Guide" it says "The Microsoft C Database, Copyright (C) 1987, by Peter Norton". Where Peter got it, I don't know.

Upvotes: 0

Related Questions