Scan codes for arrrow keys

Jitendra Kumar Chauhan 26 Reputation points
2021-11-21T04:05:56.013+00:00

Why is it printing 224 an integer value for arrows key [left,right,top and bottom] in this code,

include<stdio.h>

include<conio.h>

int main()
{
int key;
key = _getch();
printf("%d", key);
return 0;
}

while the integer value for arrows keys are 72,80,75,77. It is printing right in below code :-

include<stdio.h>

include<conio.h>

include<Windows.h>

void draw();
void gotoxy(int, int);
i = 10, j = 10;
int main()
{
printf("Press alphabet e in small case to exit the program....\n");
gotoxy(i, j);
draw();
return 0;
}
void draw()
{
int key;
while (1)
{
key = _getch();
switch (key)
{
case 75:
printf("%d",key);
i--;
break;
case 77:
i++;
break;
case 72:
j--;
break;

    case 80:
        j++;
        break;
    case 'e': 
        gotoxy(10, 70);
        exit(0);
    }
    gotoxy(i, j);
    printf("*");

}

}
void gotoxy(int row, int col)
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = {row,col };
SetConsoleCursorPosition(h, position);
}

Developer technologies C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-11-21T06:45:34.857+00:00

    According to documentation, try this:

    int key;
    key = _getch();
    if( key == 0xE0 ) // 224
    {
     key = _getch( );
    }
    printf( "%d", key );
    

    You should distinguish between Left Arrow and 'K', for example, which returns 75 too.

    2 people found this answer helpful.
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2021-11-22T23:35:39.18+00:00

    Why is it printing 224 an integer value for arrows key ...
    while ... It is printing right in below code

    Extended keys are preceded by a prefix byte, sometimes
    called an "escape scancode". So getting the value for the
    key itself requires two "_getch" executions, as illustrated
    in the example from Viorel.

    About Keyboard Input
    https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input

    Extended-Key Flag

    "The extended-key flag indicates whether the keystroke message
    originated from one of the additional keys on the enhanced
    keyboard. The extended keys consist of the ALT and CTRL keys
    on the right-hand side of the keyboard; the INS, DEL, HOME,
    END, PAGE UP, PAGE DOWN, and arrow keys in the clusters to
    the left of the numeric keypad; the NUM LOCK key; the BREAK
    (CTRL+PAUSE) key; the PRINT SCRN key; and the divide (/) and
    ENTER keys in the numeric keypad. The extended-key flag is
    set if the key is an extended key.
    If specified, the scan code was preceded by a prefix byte
    having the value 0xE0 (224)."

    Also see:

    Keyboard scancodes
    https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

    1.3 Escape scancodes

    "The codes e0 and e1 introduce scancode sequences, and are not
    usually used as isolated scancodes themselves ..."

    1.5 Escaped scancodes

    See table.

    Your second code sample "works" because of the while(1)
    loop. When the escape code prefix 0xe0 is read no match
    is found in the switch statement. But on the next iteration
    of the loop the _getch reads the unique scan code for that
    extended key. If you step through your code in the debugger
    you should see what is happening.

    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.