Example of Trapping CTRL+ALT+DEL Keys in Basic

ID: Q32788


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, versions 7.0, 7.1
  • Microsoft BASIC Compiler for MS-DOS and MS OS/2, versions 6.0, 6.0b


SUMMARY

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

  • Page 236 of "Microsoft QuickBasic 4.0: Basic Language Reference" for versions 4.0 and 4.0b


  • Page 236 of "Microsoft Basic Compiler 6.0: Basic Language Reference" for versions 6.00 and 6.00b for MS-DOS and MS OS/2


  • Page 180 of the "Microsoft Basic 7.0: Language Reference" manual for Microsoft Basic PDS versions 7.0 and 7.1


  • Page 198 of the "Microsoft QuickBasic: Basic Language Reference" 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 "Press CTRL-ALT-DEL to see trap, or 'q' to quit."
  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$ = "q"         '  Idle loop.
  END

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

ctrlaltdelwhite:
  PRINT "Pressed CTRL+ALT+DEL (white DEL key) on either keyboard"
  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 :
Version : MS-DOS:1.0,4.0,4.0b,4.5; :6.0,6.0b,7.0,7.1
Platform : MS-DOS
Issue type :


Last Reviewed: December 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.