Detecting VT100 Function Keys in an MMBasic Program
The MMBasic INKEY$ function is used within a running program to detect a character from the console.This is generally the ASCII value of that keys character. VT100 terminals generate a number of characters to signal the function keys and other functions like Page Up,Page Down,End and Home. The INKEY$ function in Picomites and latest releases of ARMmite F4 and ARMmite H7 resolve these to a single byte as shown in the table below. The table is based on codes generated by Teraterm but other VT100 emulations are similiar.
| VT100 Key | Sequence Sent | INKEY$ (ASC) | INKEY$ (HEX) |
|---|---|---|---|
| DEL | Esc[3~ (Note1) | 127 | 0x7F |
| Up Arrow | Esc[A | 128 | 0x80 |
| Dn Arrow | Esc[B | 129 | 0x81 |
| Left Arrow | Esc[D | 130 | 0x82 |
| Right Arrow | Esc[C | 131 | 0x83 |
| INS | Esc[2~ | 132 | 0x84 |
| Home | Esc[1~ | 134 | 0x86 |
| End | Esc[4~ | 135 | 0x87 |
| PgUp | Esc[5~ | 136 | 0x88 |
| PgDn | Esc[6~ | 137 | 0x89 |
| F1 | Esc[11~ | 145 | 0x91 |
| F2 | Esc[12~ | 146 | 0x92 |
| F3 | Esc[13~ | 147 | 0x93 |
| F4 | Esc[14~ | 148 | 0x94 |
| F5 | Esc[15~ | 149 | 0x95 |
| F5 | Esc[17~ | 150 | 0x96 |
| F7 | Esc[18~ | 151 | 0x97 |
| F8 | Esc[19~ | 152 | 0x98 |
| F9 | Esc[20~ | 153 | 0x99 |
| F10 | Esc[21~ | 154 | 0x9A |
| F11 | Esc[23~ | 155 | 0x9B |
| F12 | Esc[24~ | 156 | 0x9C |
| Shift F1 | n/a | ||
| Shift F2 | n/a | ||
| Shift F3 | Esc[25~ | 179 | 0xB3 |
| Shift F4 | Esc[26~ | 180 | 0xB4 |
| Shift F5 | Esc[28~ | 181 | 0xB5 |
| Shift F6 | Esc[29~ | 182 | 0xB6 |
| Shift F7 | Esc[31~ | 183 | 0xB7 |
| Shift F8 | Esc[32~ | 184 | 0xB8 |
| Shift F9 | n/a | ||
| Shift F10 | n/a | ||
| Shift F11 | n/a | ||
| Shift F12 | n/a |
Reading the Keys in MMBasic
There are two methods
char$=INKEY$
char$=Input$(100,#0)
Using CNTRL Key in MMBasic
Pressing the CNTRL key on the keyboard (directly connected or via VT100) with a normal key will result on the code for that key being masked so that only the bottom five bits (B4-B0) are used. This results in mapping those combinations to the lower non printable ascii characters 00-1F i.e. 0-31 e.g. CNTRL+C gives 3.
The CNTRL modifier is useful if the terminal being used has no function keys to allow the use of the editor.
Using CNTRL Keys in MMBasic Editor
This table shows the mapping of CNTRL + key combinations to MMBasic Editor functions.
| CNTRL +key | Mapped to | Editor Function |
|---|---|---|
| Ctrl+S | LEFT | |
| Ctrl+D | RIGHT | |
| Ctrl+E | UP | |
| Ctrl+X | DOWN | |
| Ctrl+U | HOME | |
| Ctrl+K | END | |
| Ctrl+P | PageUp | |
| Ctrl+L | PageDn | |
| Ctrl+] | DEL | |
| Ctrl+N | INSERT | |
| Ctrl+Q | F1 | SAVE |
| Ctrl+W | F2 | RUN |
| Ctrl+R | F3 | FIND |
| Ctrl+G | Shift-F3 | REPEAT FIND |
| Ctrl+T | F4 | MARK/CUT |
| Ctrl+Y | F5 | PASTE/COPY |
ReadConsole Subroutine
The SUB below can be called from the main DO:LOOP to interact with a running program from an attached terminal.This can be useful for debugging and developing a hardware interface that requires the program to be running.
SUB ReadConsole
local char$
' j=LOC(#0)
'char$= INPUT$(100,#0)
char$=INKEY$
IF char$<>"" then
if char$=chr$(145) then print "F1" 'F1 VT100
if char$=chr$(146) then print "F2" 'F2 VT100
if char$=chr$(147) then print "F3" 'F3 VT100
'
'
if char$=chr$(156) then print "F12" 'F12 VT100
if char$=chr$(127) or mid$(char$,2)="[3~" then 'DEL
end if
if char$=chr$(128) or mid$(char$,2)="[A" then 'up arrow VT100
end if
if char$=chr$(129) or mid$(char$,2)="[B" then 'down arrow
end if
if char$=chr$(131) or char$=chr$(4) or mid$(char$,2)="[C" then 'right arrow VT100 MMEdit
end if
if char$=chr$(130) or char$=chr$(19)or mid$(char$,2)="[D" then 'left arrow VT100 VT100 MMEdit
end if
if char$=chr$(132) or mid$(char$,2)="[2~" then 'INS
end if
if char$=chr$(133) then
print "133"
end if
if char$=chr$(134) or mid$(char$,2)="[1~" then 'home
end if
if char$=chr$(135) or mid$(char$,2)="[4~" then 'end
end if
if char$=chr$(136) or mid$(char$,2)="[5~" then 'PgUp VT100
end if
if char$=chr$(137) or mid$(char$,2)="[6~" then 'Pgdn VT100
end if
if char$=chr$(138) then
print "138"
end if
if char$=chr$(139) then
print "ALT"
end if
if char$=chr$(140) then
print "140"
end if
END IF
END SUB
