Trapping a Keystroke and Displaying Its Extended Scan Code

Last reviewed: January 6, 1995
Article ID: Q64439
The information in this article applies to:
  • Microsoft Macro Assembler for MS-DOS, versions 5.1 and 5.1a

SUMMARY

The following example causes a main C program to call an assembly routine to print the ASCII code and the extended code resulting from a keystroke.

For example, if you press the A key, the output is an ASCII 61 and an extended code of 1E indicating the position of the key. If F1 is pressed, there is no ASCII equivalent for it, and a 0 is returned for the ASCII code and hex 3B for the extended code.

MORE INFORMATION

A keystroke is sensed by the keyboard microprocessor that deposits an 8-bit scan code at Port A of the 8255 peripheral interface chip. (Bit 1=1 when the key contact is made; 0 when the key is released.)

Following the scan code, keyboard Interrupt 9H is invoked and a 2-byte character code is placed in the keyboard buffer. The status of the SHIFT and toggle keys is stored in memory and is checked before the character code is generated. This allows for uppercase or lowercase characters.

The upper byte of the character code contains an ASCII code if one is generated; the lower byte contains the extended code, depending on the position of the key pressed.

A few key combinations do not generate scan codes: <CTRL+BREAK>, <CTRL+ALT+DEL>, <PRINT SCREEN>, and <ALT+SYSRQ> on a 286 computer. These combinations cause special predefined results.

The following is the example:

Sample Code:

/* Compile options needed: none
*/

#include <stdio.h>
extern key_read( int*, int* );

void main( )
{
   int *ascii_ptr, *scan_ptr, num, num1;
   num = 0;
   num1 = 0;
   ascii_ptr = &num;                 // initialize pointers to zero
   scan_ptr = &num1;

   key_read( ascii_ptr, scan_ptr );  // call assembly routine

// print the high byte - ASCII code, and the low byte - extended
// code of the character placed in the keyboard buffer

   printf( "The ASCII code is hex %x or decimal %d\n", *ascii_ptr,"
           " *ascii_ptr);
   printf( "The EXTENDED Code is hex %x "
           "or decimal %d\n", *scan_ptr, *scan_ptr);
}

******************************************************

; Assemble options needed: none

         .model small,c
         .data

         .code
PUBLIC   key_read
key_read PROC
         PUSH  bp          ;save the base pointer
         MOV   bp, sp

; Invoke Int 21h Function Ch to clear the keyboard buffer before ; accepting a keystroke.

         MOV   ah, 0CH
         MOV   al, 0
         INT   21h

; Invoke Int 16h Function 0h to place the character code in the AX ; register.

         MOV   ah, 0H
         INT   16H

         MOV   bx, [bp+4]  ;ASCII returned
         MOV   [bx], al

         MOV   bx, [bp+6]  ;Extended code returned
         MOV   [bx], ah

         POP   bp
         RET
key_read ENDP

         END


Additional reference words: 5.10 5.10a
KBCategory: kbprg
KBSubCategory: MASMLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 6, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.