You are currently offline, waiting for your internet to reconnect

How to Just Trap CTRL, ALT, or SHIFT Keys in Basic

This article was previously published under Q59131
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Normally, to trap CTRL (CONTROL), ALT, and SHIFT in combination withother keys, you would set the bits for these keys in the first byte(keyboard flag) of the two CHR$() bytes in the KEY statement.

However, to trap the CTRL, ALT, and SHIFT keys alone (not incombination with other keys), the keyboard flag in the KEY statementshould be CHR$(0) for the standard keyboard or CHR$(&H80) for keysspecific to an extended keyboard.
The syntax of the KEY statement (where n=15 through 25 can beuser-defined keys) is as follows:
KEY n, CHR$(keyboardflag) + CHR$(scancode)
The following table lists the scan codes for the CTRL, ALT and SHIFTkeys:
   Key           Scan Code   ---           ---------   CTRL          &H1D   ALT           &H38   LEFT SHIFT    &H2A   RIGHT SHIFT   &H36				
NOTE: To trap the RIGHT CTRL or RIGHT ALT key (found only on anextended 101 keyboard), the extended keyboard flag (&H80) must be usedin the keyboard flag byte.

Code Example

The following code example traps the CTRL, ALT, and SHIFT keysalone:
' NOTE:  This program requires NUM LOCK and CAPS LOCK to be off.KEY 15, CHR$(&H0) + CHR$(&H1D)     ' LEFT CTRL (onON KEY(15) GOSUB Ctrl              ' Standard or Extended keyboard).KEY(15) ONKEY 16, CHR$(&H80) + CHR$(&H1D)    ' RIGHT CTRL (on Extended keyboard).ON KEY(16) GOSUB CtrlKEY(16) ONKEY 17, CHR$(&H0) + CHR$(&H38)     ' LEFT ALT (onON KEY(17) GOSUB alt               ' Standard or Extended keyboard).KEY(17) ONKEY 18, CHR$(&H80) + CHR$(&H38)    ' RIGHT ALT (on Extended keyboard).ON KEY(18) GOSUB AltKEY(18) ONKEY 19, CHR$(&H0) + CHR$(&H2A)     ' LEFT SHIFT (onON KEY(19) GOSUB Shift             ' Standard or Extended keyboard).KEY(19) ONKEY 20, CHR$(&H0) + CHR$(&H36)     ' RIGHT SHIFT (onON KEY(20) GOSUB Shift             ' Standard or Extended keyboard).KEY(20) ONWHILE INKEY$ <> CHR$(27): WEND     ' Press ESC to end.ENDCtrl: PRINT "CTRL"RETURNAlt:  PRINT "ALT"RETURNShift:  PRINT "SHIFT"RETURN				
VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 6.00 6.00b 7.00

Article ID: 59131 - Last Review: 08/16/2005 21:23:42 - Revision: 2.1

Microsoft Visual Basic for MS-DOS, 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

  • KB59131