Microsoft KB Archive/64439

= Trapping a Keystroke and Displaying Its Extended Scan Code =

Article ID: 64439

Article Last Modified on 10/17/2003

-

APPLIES TO


 * Microsoft Macro Assembler 5.1 Standard Edition
 * Microsoft Macro Assembler 5.1a

-



This article was previously published under Q64439



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: , , , and  on a 286 computer. These combinations cause special predefined results.

The following is the example:

Sample Code:
/* Compile options needed: none

extern key_read( int*, int* );
 * 1) include 

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( &quot;The ASCII code is hex %x or decimal %d\n&quot;, *ascii_ptr,&quot;          &quot; *ascii_ptr); printf( &quot;The EXTENDED Code is hex %x &quot;          &quot;or decimal %d\n&quot;, *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 query words: 5.10 5.10a

Keywords: KB64439

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.