Microsoft KB Archive/45909

= Example of How to Use 1- and 2-Byte Return Codes from INKEY$ =

Article ID: 45909

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q45909



SUMMARY
The following information applies to QuickBasic Versions 3.00, 4.00, 4.00b, and 4.50, to Microsoft Basic Compiler Versions 6.00 and 6.00b, and to Microsoft PDS Version 7.00. The Version 4.50 QuickBasic Advisor and the Basic PDS 7.00 Microsoft Advisor on-line Help system state the following:

The INKEY$ function returns a 1- or 2-byte string containing a character read from the standard input device. A null string is returned if no character is waiting there. A 1-character string contains the actual character read from the keyboard, while a 2-character string indicates an extended code, the first character of which is hexadecimal 00.

When two bytes are received from an extended key, the second character of the string is the scan code associated with the extended key. The chart in the Version 4.50 QuickBasic Advisor and Microsoft Advisor on-line Help system for Basic PDS 7.00 contains the scan code listing. The extended keys include the function keys, arrow keys, HOME, PGUP, END, PGDN, and SHIFT+TAB keys.



MORE INFORMATION
The following program example demonstrates how to use the INKEY$ function to return either a 1-byte character or 2-byte extended code. The length, ASCII representation, and the numeric representation for each key that is pressed are displayed. In addition, the arrow keys and the SHIFT+TAB key combination are trapped for 2-byte returns, and the ESC, TAB, and SPACEBAR are trapped for 1-byte returns.

Code Example
'SCAN CODES to be used with a 2-byte return code from INKEY$ CONST left = &H4B CONST right = &H4D CONST up = &H48 CONST down = &H50 CONST tabscan = 15

'ASCII CODES to be used with a 1-byte return from INKEY$ CONST escape = 27 CONST tabchar = 9 CONST space = 32

DO UNTIL UCASE$(t$) = &quot;Q&quot; 'PROGRAM ENDS WHEN 'Q' OR 'q' IS ENTERED CLS LOCATE 23, 35 PRINT &quot;Q to quit&quot; t$ = INKEY$ IF t$<>&quot;&quot; THEN LOCATE 10, 1 length% = LEN(t$) PRINT &quot;length &quot;; length% PRINT &quot;ASCII representation &quot;; t$ PRINT &quot;numeric representation &quot;; SELECT CASE length% CASE 2 FOR i = 1 TO 2 PRINT ASC(MID$(t$, i, 1)); &quot; &quot;; NEXT i                SELECT CASE ASC(RIGHT$(t$, 1)) CASE up                         PRINT &quot;up           &quot; CASE down PRINT &quot;down        &quot; CASE left PRINT &quot;left        &quot; CASE right PRINT &quot;right       &quot; CASE tabscan PRINT &quot;Shift tab&quot; END SELECT CASE 1 PRINT ASC(t$); SELECT CASE ASC(LEFT$(t$, 1)) CASE escape PRINT &quot;escape      &quot; CASE tabchar PRINT &quot;tab character&quot; CASE space PRINT &quot;space       &quot; END SELECT CASE ELSE PRINT &quot;                   &quot; END SELECT END IF LOOP END

Additional query words: QuickBas BasicCom

Keywords: KB45909

-

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

© Microsoft Corporation. All rights reserved.