Classic Computer Magazine Archive COMPUTE! ISSUE 64 / SEPTEMBER 1985 / PAGE 10

 
Readers Feedback

 The Editors and Readers of COMPUTE!



Trapping IBM's Break Key
I own an IBM PC and have been trying to trap the Ctrl-Brk keys. I have looked in a tremendous number of books, but still couldn't find anything about it. I haven't been able to scan the keyboard for the information I need. How can I trap those keys?
Patrick McGarry

Since many readers have asked this question, we'll show you two techniques that work with BASICA or Cartridge BASIC on either the PC or PCjr. The following program traps both Ctrl-Break (break) and Ctrl-Alt-Del (reboot).

10 CLS:PRINT "Try to use Brea
   k or Ctrl-Alt-Del"
20 B$=CHR$(4)+CHR$(70):C$=CHR
   $(12)+CHR$(83)
30 KEY 15,B$:KEY (15) ON:ON K
   EY (15) GOSUB 80
40 KEY 16,C$:KEY (16) ON:ON K
   EY (16) GOSUB 90
50 FOR J=1 TO 9999:NEXT:PRINT
    "Break & Ctrl-Alt-Del wor
   k now"
60 KEY (15) OFF:KEY (16) OFF
70 GOTO 70
80 PRINT "Break has no effect
    right now.":RETURN
90 PRINT "Rebooting is a very
    bad idea.":RETURN

    Once the key trap is set (lines 20-40 above), the system checks for a trap between every statement of the main program. When the right keys are pressed, execution diverts immediately to the trapping subroutine, no matter what the main program is doing at the time. Since the trap can be sprung between any two statements in the program, strange results may occur if you don't anticipate the possible diversion. Of course, the trapping subroutine doesn't have to print a message (or do anything else except end with RETURN). You can also disable Break by changing the computer's break interrupt vector at locations 108-112 (&H6C-&H6F), as shown here:

10 DEF SEG=0:FOR J=0 TO 3:A(J
   )=PEEK(108+J):NEXT
20 POKE 108,64:POKE 109,1:POK
   E 110,112:POKE 111,0
30 PRINT "Try to use Ctrl-Brk
    (PC) or Fn-Brk (PCjr)
40 FOR J-1 TO 9999:NEXT:PRINT
    "Brk key works again"
50 FOR J=0 TO 3:POKE 108+J,A(
   J):NEXT
60 GOTO 60

    This program diverts the system's normal break routine to a do-nothing IRET (return) instruction in ROM (Read Only Memory). Don't forget to restore the normal vector when the program ends (line 50). These examples are drawn from Russ Davies' Mapping the IBM PC and PCjr (published by COMPUTE! Books), which contains additional information on keyboard programming from DOS and machine language.