Category : C Source Code
Archive   : SCANCODE.ZIP
Filename : SCANCODE.C

 
Output of file : SCANCODE.C contained in archive : SCANCODE.ZIP
/* scancode.c -- uses interrupt 22 to read IBM keyboard scancodes */
/* Written by George A. Stanislav, February 17, 1987 for strictly */
/* educational purposes. */

#include
#include /* For MicroSoft C */
#define BIOS_CALL 0x16 /* hexadecimal value of 22 */
#define RIGHT 0x01 /* right shift key mask */
#define LEFT 0x02 /* left shift key mask */
#define CTRL 0X04 /* control key mask */
#define ALT 0x08 /* alt key mask */
#define SCROLL 0x10 /* scroll lock mask */
#define NUMBER 0x20 /* number lock mask */
#define CAPS 0x40 /* caps lock mask */
#define INSERT 0x80 /* insert state mask */

main()
{
unsigned code, shkey;
unsigned scancode(), shift();

printf("Press a key to get its scan code.\nPress to end.\n");
while ((code=scancode()) != 1)
{
printf("\nThe scan code of the key you pressed is %u, or %#x.\n",code,code);
if ((shkey = shift()) != 0)
{
if ((shkey & RIGHT) != 0)
printf("Right shift key is depressed.\n");
if ((shkey & LEFT) != 0)
printf("Left shift key is depressed.\n");
if ((shkey & CTRL) != 0)
printf("Control key is depressed.\n");
if ((shkey & ALT) != 0)
printf("Alt key is depressed.\n");
if ((shkey & SCROLL) != 0)
printf("Scroll state is active.\n");
if ((shkey & NUMBER) != 0)
printf("Number lock is engaged.\n");
if ((shkey & CAPS) != 0)
printf("Caps lock is engaged.\n");
if ((shkey & INSERT) != 0)
printf("Insert state is active.\n");
}
}
printf("\nThe scan code for is %u, or %#x.\n\nBye.",code,code);
}

unsigned scancode()
{
union REGS regin, regout; /* input, output register values */
unsigned code; /* scan code */

regin.h.ah = 0; /* will wait till a key is pressed, read it, */
/* return its scan code and remove it from keyboard */
/* buffer. To read it WITHOUT removing it from the */
/* buffer (so it can be scanf() later), use 1 instead */
int86(BIOS_CALL, ®in, ®out);
code = regout.h.ah; /* the scan code is in the AH register */
return code;
}

unsigned shift()
{
union REGS regin, regout; /* input, output register values */
unsigned shstatus; /* shift status */

regin.h.ah = 2; /* gets the status of all "shift" keys */
int86(BIOS_CALL, ®in, ®out);
shstatus = regout.h.al; /* the status is in the AL register */
return shstatus;
}



  3 Responses to “Category : C Source Code
Archive   : SCANCODE.ZIP
Filename : SCANCODE.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/