You are currently offline, waiting for your internet to reconnect

How to Trap PRINT SCREEN Key on an Extended (101-Key) Keyboard

This article was previously published under Q59133
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.
You can detect (trap) the PRINT SCREEN (or SHIFT+PRINT SCREEN, or PRTSCR, and so on) key using the ON KEY GOSUB statement. This techniquedoes not work under Microsoft Windows, and may not work reliably onall computers (it depends upon the type of ROM BIOS).

Although Basic can detect the key press with the ON KEY GOSUBstatement, the PRINT SCREEN action is controlled through the ROM BIOSinterrupt table, and cannot be disabled directly with Basic. Todisable the print screen utility, the interrupt vector must beremapped with a C or assembly routine.
Here is the syntax of the KEY statement, where n=15-25 can beuser-defined key combinations to trap:
   KEY n, CHR$(keyboardflag) + CHR$(scancode)				
The following chart shows the keyboard flags and scan codes for PRINTSCREEN and SHIFT+PRINT SCREEN for standard and extended keyboards:
                          Standard   Extended                          --------   --------   Keyboard flag for:      No    SHIFT         &H00       &H80      LEFT  SHIFT         &H02       &H82      RIGHT SHIFT         &H01       &H81   Scan code for:      PRINT SCREEN        &H37       &H2A				
Note: The keyboard flags are affected by CAPS LOCK and NUM LOCK. Theabove values assume that CAPS LOCK and NUM LOCK are off. Add &H40 tothe keyboard flag for CAPS LOCK, and add &H20 for NUM LOCK.

Code Example

The following code example detects each of the PRINT SCREEN andSHIFT+PRINT SCREEN combinations listed above:
'Note: This program detects PRINT SCREEN and SHIFT+PRINT SCREEN key'      combinations on standard and extended keyboards with CAPS LOCK'      and NUM LOCK off.''Also note: This program does NOT disable the PRINT SCREEN action'           (that is, the screen is still printed). See the explanation'           in the text of the above article.KEY 15, CHR$(&H00) + CHR$(&H37)        'Standard no SHIFTsKEY 16, CHR$(&H02) + CHR$(&H37)        'Standard LEFT SHIFTKEY 17, CHR$(&H01) + CHR$(&H37)        'Standard RIGHT SHIFTKEY 18, CHR$(&H80) + CHR$(&H2A)        'Extended no SHIFTsKEY 19, CHR$(&H82) + CHR$(&H2A)        'Extended LEFT SHIFTKEY 20, CHR$(&H81) + CHR$(&H2A)        'Extended RIGHT SHIFTFOR i%=15 TO 20  ON KEY(i%) GOSUB Handle:  KEY(i%) ONNEXTWHILE INKEY$<>CHR$(27) : WEND        'Press ESC to endENDHandle: PRINT "Trapped PRINT SCREEN keystroke"        PRINT "Screen will still print unless"        PRINT "Interrupt vector changed with"        PRINT "C or Assembly routine."        RETURN				
QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Article ID: 59133 - Last Review: 08/16/2005 21:24:12 - Revision: 2.1

  • 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
  • Microsoft BASIC Professional Development System 7.1
  • KB59133