What does getch() really get? Scan codes?
Asked Answered
A

1

7
#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

Abuttal answered 10/1, 2014 at 12:35 Comment(7)
is that really the code you are using? it should print a zero padded decimal number of 3 digits followed by the hex. i'm curious that there isn't a bug somewhere since 0x86 seems only unproducible. what happens for the num pad pg up?Viipuri
@Viipuri Sorry. I edited the code.Abuttal
@KevinDongNaiJia and that gives you these exact results? the only way i can see 0x86 produced is from set 1, when the 5 key is release (0x80 + 0x06) and this would not be preceded by e0.Viipuri
@Viipuri They should be preceded by 0xE0. Visit here.Abuttal
@KevinDongNaiJia the particular example i gave (from set 1, not your case) would not include 0xe0 - pg up and del both do. What about num pad pg up? what does that give you?Viipuri
@Viipuri Sorry, I am using laptop. :-(Abuttal
What compiler are you using? MSDN web page about _getch implies it's returning ANSI or UNICODE, not scan codes. Take a look at the example that looks for 'y' or 'Y' .Eduino
D
0

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.

Drove answered 10/1, 2014 at 16:35 Comment(4)
Andries Brouwer has a detailed discussion of different keyboard scancodes at his site: win.tue.nl/~aeb/linux/kbd/scancodes-1.htmlDrove
Forgive me if I'm wrong, but that table seems similar to other standard tables I've found, and in all of them there is no 0x86Viipuri
crud, you're right. I was looking for the 86, but Google hits kept coming up with other things (x86), so then I focused on page-down codes instead of page-up codes, and forgot to check both at the end.Drove
Well from the look of the table gray and white ctrl pageup are equal - maybe the laptop just uses a different scan code, to differentiate (even though it only has one pageup). Maybe if OP posted more scan codes..Viipuri

© 2022 - 2024 — McMap. All rights reserved.