Scan codes of arrow keys using C ??

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<dos.h>

getkey();

void main()

{

clrscr();

int x; /* to save the value of scan code returned by getkey()*/

x=getkey();

printf("%d",&x);

getch();

}

getkey()

{

union REGS i,o;

while(!kbhit())

;

i.h.ah=0;

int86(22,&i,&o);

return(o.h.ah);

}

Every time I run this code and press any key(even arrow keys) i get a value -12 i.e. minus 12.

i know that for arrow keys scan codes are 72,80,75,77 but i the function getkey is of no help .

Please tell me whats wrong with getkey function.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    Don't use the DOS interrupt calls. That's old school. Use proper IO routines.

    In this use _getch() (or getche() if you want echo). Call the function twice for arrow keys. The first call will return 00 or 0E. The second call will return the actual key code.

    int c = getch();

    if (c == 0x00 or c == 0x0e)

    {

    // get extended key code

    c = getch();

    // use extended key code here

    }

    else

    {

    // c is regular key

    }

Still have questions? Get your answers by asking now.