IBM Advanced Function Key Techniques (COMPUTE! Magazine January 1986 by Peter F. Nicholson, Jr.) Anyone who has ever redefined the function keys in an IBM BASIC program probably has wondered why there's no command to restore the keys' original definitions when the program ends. Usually you end up disabling them or redefining them again to their default values. But there is an alternative, and the secret lies within something called the soft key buffer. Locating and examining this buffer can yield some interesting results. Finding the buffer is easy if you have an IBM PC, XT or PCjr. It starts at memory location 1619 in the default memory segment. But this is not necessarily true if you have an IBM-compatible computer. Therefore, if you're using a compatible, you should run Program 1. This program attempts to locate the soft key buffer for you. When you find it, you should alter the buffer address (1619) in the IBM programs before running them on your compatible. The lines where this address can be found are indicated in REMark statements within each program. The soft key buffer is just a section of memory which stores the definitions for the function keys. When a key is assigned a different function, its definition within the buffer is altered. A key definition can contain up to 15 characters. If you PEEK into the buffer's memory locations, you may be surprised to find that each key is assigned not 15, but 16 positions. Knowing the number of positions allotted for each function key makes it easy to save the buffer's contents, and therefore to preserve the keys' definitions. Program 2 does this by reading the contents of the buffer into an array. Then it assigns new functions to the keys. Finally, the program lets you restore the original functions by POKEing the contents of the array back into the soft key buffer. You can use this technique in your own programs to restore the function keys. If you're wondering why each key is assigned 16 positions in the buffer when its definition can be only 15 characters long, disabling the keys will provide the answer. If you PEEK at the 16 positions reserved for F1 (originally defined as LIST) and print out the ASCII values, this is what you'll see: L I S T 0 0 0 0 0 0 0 0 0 0 0 0 When you disable F1, the buffer is: 0 I S T 0 0 0 0 0 0 0 0 0 0 0 0 This seems to indicate that BASIC marks the end of a function key definition with a zero. To prove this, run Program 3. It demonstrates that you can restore the function keys after disabling them by merely saving the first character of each key definition (assuming, of course, that the keys have been disabled by overwriting only the first character of the definition). That's why Program 3 needs to save only 10 bytes instead of the 160 bytes saved by Program 2. Knowing that you can restore the disabled function keys by saving only the first character of each definition may be interesting, but the difference between 10 and 160 bytes probably is of little concern. The real power in this knowledge is that you can extend the number of characters available for a function key's definition by altering the 16th position in the buffer for that key. This lets you assign a longer definition to a function key (at the expense of the following key, however). For instance, you may prefer to edit programs in SCREEN 0,0,0 and WIDTH 80. Using Program 4, you can set F9 to execute these commands even though they exceed 15 characters. F10 becomes useless, since the size of the soft key buffer hasn't been increased -- just the length of F9's definition within that buffer. Program 4 also lets you save the new function key definitions as a file which can be BLOADed from another program. If you try this, don't omit the buffer address (1619) when BLOADing the file, since there is no way to insure that BASIC's segment will be the same as when you originally created the file. Program 1: Buffer Finder for Compatibles 100 DEF SEG:SCREEN 0:WIDTH 80:X=0 110 CLS:PRINT "MEMORY LOCATION ";:LOCATE ,20 120 KEY 1,"LIST":A=ASC("L") 130 IF PEEK(X)=A THEN GOSUB 150 ELSE PRINT X;:LOCATE 1,20 140 X=X+1:GOTO 130 150 IF CHR$(PEEK(X+1))<>"I" THEN RETURN 160 IF CHR$(PEEK(X+2))<>"S" THEN RETURN 170 IF CHR$(PEEK(X+3))<>"T" THEN RETURN 180 CLS:PRINT "MEMORY LOCATION ";X 190 FOR J=1 TO 10:PRINT "F";J;:FOR K=0 TO 15 200 IF PEEK(X+16*(J-1)+K)>0 THEN PRINT CHR$(PEEK(X+16*(J-1)+K)); ELSE 220 210 NEXT K 220 PRINT:NEXT J 230 BEEP:INPUT "IS THIS IT ";Q$ 240 IF Q$="Y" OR Q$="y" THEN END ELSE X=X+1:CLS:GOTO 110 Program 2: Restoring Function Definitions 90 'Lines which use 1619 offset are 140 and 250 100 SCREEN 0:WIDTH 80:CLS:DEF SEG:OPTION BASE 1 110 KEY ON:DIM K$(10):FOR X=1 TO 10:K$(X)=STRING$(16,0):NEXT X 120 'Save function keys 130 FOR X=1 TO 10:FOR J=0 TO 15 140 MID$(K$(X),J+1,1)=CHR$(PEEK(1619+16*(X-1)+J)) 150 NEXT J,X 160 'Redefine function keys with letters (example follows) 170 FOR X=1 TO 10:KEY X,CHR$(X+64):NEXT X:KEY LIST 180 PRINT "Function keys are redefined":PRINT "Press any key to restore" 190 KB$=INKEY$:IF KB$="" THEN 190 200 'Restore function keys 210 FOR X=1 TO 10 220 KEY X,K$(X) 230 NEXT X:CLS 240 FOR X=1 TO 10 250 J=ASC(MID$(K$(X),16,1)):IF J>0 THEN POKE 1619+16*(X-1)+15,J 260 NEXT X:CLS 260 KEY LIST Program 3: Restoring Function Definitions 90 'Lines which use 1619 offset are 140 and 220 100 SCREEN 0:WIDTH 80:CLS:DEF SEG 110 KEY ON:K$=STRING$(10,0) 'Storage area for function keys 120 'Save function keys 130 FOR X=1 TO 10 140 MID$(K$,X,1)=CHR$(PEEK(1619+16*(X-1))) 150 NEXT X 160 'Disable function keys 170 FOR X=1 TO 10:KEY X,"":NEXT X:KEY LIST 180 PRINT "Function keys are disabled":PRINT "Press any key to restore" 190 KB$=INKEY$:IF KB$="" THEN 190 200 'Restore function keys 210 FOR X=1 TO 10 220 POKE 1619+16*(X-1),ASC(MID$(K$,X,1)) 230 NEXT X:CLS 240 KEY LIST Program 4: Extending Definitions 90 'Lines which use 1619 offset are 180, 290, 390, 440, 470 100 DEF SEG:STK$=STRING$(128,0):SCR$=STRING$(37,0:RESTORE 110:FOR X=1 TO 37:READ J:MID$(SCR$,X,1)=CHR$(J):NEXT X:SCR!=PEEK(VARPTR(SCR$) +1)+256*PEEK(VARPTR(SCR$)+2) 110 DATA 85,137,229,139,118,6,41,192,138,4,139,116,1 120 DATA 1,240,137,196,184,0,6,187,0,7,185,0,2 130 DATA 186,80,24,85,205,16,92,93,202,2,0 140 SCREEN 0:WIDTH 80:CLS 150 T$="Function Key Definition" 160 LOCATE 2,(40-.5*LEN(T$)):PRINT T$ 170 PRINT:PRINT 180 X=1:J=1:K=1619 190 K$=STRING$(160,0):KN$=STRING$(160,0):K=K-1 200 L=PEEK(J+K) 210 WHILE L<>0 220 MID$(K$,J,1)=CHR$(L) 230 J=J+1:L=PEEK(J+K) 240 WEND 250 PRINT "Function Key ";X;": ";MID$(K$,1,J-1) 260 PRINT:PRINT "Enter new definition or press Enter to leave unchanged" 270 LINE INPUT Q$:IF LEN(Q$)>0 THEN GOSUB 300:IF ER=1 THEN ER=0:GOTO 250 280 IF X+FIX(J/16)>9 THEN GOTO 380 290 X=X+1+FIX(J/16):K=1619+16*(X-1)-1:J=1:CALL SCR!(STK$):LOCATE 5,1: GOTO 200 300 INPUT "Do you want a carriage return (Y/N)?;Q1$ 310 IF Q1$="Y" OR Q1$="y" THEN Q$=Q$+CHR$(13) 320 IF LEN(Q$)<16 THEN J=LEN(Q$):KEY X,Q$:RETURN 330 M=1:N=16*(X-1)+1:IF N+LEN(Q$)>160 THEN BEEP:PRINT "Too long":ER=1: RETURN 340 MID$(KN$,N,1)=MID$(Q$,M,1) 350 M=M+1:N=1+N:IF M<=LEN(Q$) THEN 340 360 IF LEN(Q$)>J THEN J=LEN(Q$) 370 RETURN 380 FOR X=1 TO 10 390 IF ASC(MID$(KN$,16*(X-1)+1,1))>0 THEN FOR J=16*(X-1)+1TO 16*X:POKE 1619+J-1,ASC(MID$(KN$,J,1)):NEXT J 400 NEXT X:CLS:KEY LIST 410 KB$=INKEY$:IF KB$="" THEN 420 ELSE 410 420 PRINT:INPUT "Do you want to save function keys as a BLOADable file (Y/N)?";q$ 430 IF Q$="Y" OR Q$="y" THEN INPUT "Filename";F$ ELSE END 440 BSAVE F$,1619,159:PRINT 450 PRINT "To load your function key file, use these commands:" 460 PRINT:PRINT 470 PRINT "DEF SEG:BLOAD ";CHR$(34);F$;CHR$(34);",1619:CLS":END