Microsoft KB Archive/32788

= Example of Trapping CTRL+ALT+DEL Keys in Basic =

Article ID: 32788

Article Last Modified on 8/16/2005

-

APPLIES TO


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

-



This article was previously published under Q32788



SUMMARY
The correction below applies to the KEY statement in the following manuals:


 * Page 236 of &quot;Microsoft QuickBasic 4.0: Basic Language Reference&quot; for versions 4.0 and 4.0b
 * Page 236 of &quot;Microsoft Basic Compiler 6.0: Basic Language Reference&quot; for versions 6.00 and 6.00b for MS-DOS and MS OS/2
 * Page 180 of the &quot;Microsoft Basic 7.0: Language Reference&quot; manual for Microsoft Basic PDS versions 7.0 and 7.1
 * Page 198 of the &quot;Microsoft QuickBasic: Basic Language Reference&quot; manual for QuickBasic version 4.5

The following phrase for the KEY(n) statement is incorrect:

...a keyboardflag value of &H12 would test for both CTRL and ALT being pressed.

The keyboardflag value should be &H0C on a non-extended keyboard, not &H12, to test for both CTRL and ALT being pressed. The keyboardflag value should be &H8C on an extended keyboard. This example incorrectly uses decimal addition on hexadecimal numbers.



MORE INFORMATION
The following Basic program gives an example of trapping the CTRL+ALT+DEL keystroke sequence for both extended and non-extended keyboards. ' To try this example in VBDOS.EXE: ' 1. From the File menu, choose New Project. ' 2. Copy the code example to the Code window. ' 3. Press F5 to run the program. ' ' This example may not work correctly on some computers. ' &H80 = keyboard flag value to add for extended keyboard keys ' &H0C = keyboard flag for CTRL (&H04) plus ALT (&H08), pressed '       together. ' &H53 = scan code for DELETE (or DEL) key CLS PRINT &quot;Press CTRL-ALT-DEL to see trap, or 'q' to quit.&quot; KEY 15, CHR$(&HC) + CHR$(&H53)   '   Trap CTRL+ALT+DEL for ON KEY(15) GOSUB ctrlaltdelwhite '   white DEL key. KEY(15) ON KEY 16, CHR$(&H8C) + CHR$(&H53)   '   Trap CTRL+ALT+DELETE for ON KEY(16) GOSUB ctrlaltdelgrey  '   grey (extended) DELETE key. KEY(16) ON DO  LOOP UNTIL INKEY$ = &quot;q&quot;         '  Idle loop. END

ctrlaltdelgrey: PRINT &quot;pressed CTRL+ALT+DELETE (grey DEL key) on extended keyboard&quot; RETURN

ctrlaltdelwhite: PRINT &quot;Pressed CTRL+ALT+DEL (white DEL key) on either keyboard&quot; RETURN Please note that when you run this program, pressing CTRL+ALT+DEL will reboot the computer if any of the following key states are also active:

SHIFT, NUM LOCK, or CAPS LOCK

You must define separate ON KEY(n) statements for trapping CTRL+ALT+DEL in combination with the different states of the SHIFT, NUM LOCK, or CAPS LOCK keys. In the ON KEY(n) statement, n can be 15 through 25; this limits you to 11 user-defined keys.

Note: On some machines, even if you trap all combinations of the NUM LOCK, SHIFT and CAPS LOCK keys along with CTRL+ALT+DEL, the trap will fail and the machine will reboot. This occurs because the CTRL+ALT+DEL scancode combination on some computers is configured to a hardware interrupt that occurs before and below Basic's software trap, effectively bypassing the trap and rebooting the machine.

The keyboardflag value &H0C in the KEY statement is obtained by adding together the keyboardflag values from the above pages for the CTRL and ALT keys, as in the following example:   &H04   +  &H08   =>  &H0C (CTRL)   (ALT)      (keyboardflag for KEY statement) When adding together keyboardflag values to trap different combinations of SHIFT, CTRL, ALT, NUM LOCK, CAPS LOCK, or Advanced-101-keyboard extended keys, it is important to remember that the above values are in hexadecimal (base 16) notation, where numbers are preceded with &H. If you wish, you can convert the number to decimal notation (base 10) and use that value. If you are using decimal notation, be sure not to use &H in front of the value in Basic.

Additional query words: VBmsdos QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10 restart

Keywords: KB32788

-

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

© Microsoft Corporation. All rights reserved.