Platform-independent keypresses =============================== BBC BASIC can run on many platforms. While the normal 'printable' characters and control characters generate the same codes on all platforms, some of these use different key values for top-bit keypresses - function and editing keys. The most common need is to test for cursor keys. This can be made platform independent with: * *FX 4,1 to have cursor keys generate compatible codes on all platforms * use key% AND &BB to test for cursor keys On Acorn platforms: *FX 4,1 sets cursors to generate &88-&8B key% AND &BB gives &88-&8B On DOS/Windows: *FX 4,1 is ignored, cursors generate &88-&8B key% AND &BB gives &88-&8B cZ80Tube and other code that uses the Console library: *FX 4,1 is ignored, cursors generate &CC-&CF key% AND &BB gives &88-&8B Brandy Basic: *FX 4,1 is ignored, cursors generate &CC-&CF key% AND &BB gives &88-&8B Similarly, key% AND 11 converts cursor keypresses into the equivalent VDU characters, VDU 8,9,10,11 Sample code: *FX 4,1 REPEAT key%=GET IF key%>&8F THEN PROCcursor(key% AND 9) other stuff etc. ... ...add Electron bit Chatting with Dave H earlier reminded me of a programming tip. On the Electron the editing keys are shared with character keys, so you normally can't detect modified editing keys (eg Shift+Dowm, Ctrl+Right, etc) in the same way as on other computers by using *FX4,1 or *FX4,2. However, if your program doesn't use the [ ^ | _ ` ] ~ \ } { keys then you can test for them and translate them to the normal editing keycode or character codes. The first method is to translated them to the *FX4,1 character codes. In program startup use: [b]*FX4,1[/b] When reading keypresses use: [b]K%=GET IF INKEY-256=1:A%=INSTR("[^|_`]~\}{",CHR$K%)-1:IF A%>-1:K%=&87+A%MOD5[/b] This translates modified editing keys to the standard *FX4,1 values of &87-&8B, and you can test for SHIFT and CTRL with the normal INKEY-1 and INKEY-2 calls, as in this demo: [b] 10 REM Electron cursor keys with *FX4,1 20 : 30 *FX4,1 40 REPEAT 50 K%=GET 60 IF INKEY-256=1:A%=INSTR("[^|_`]~\}{",CHR$K%)-1:IF A%>-1:K%=&87+A%MOD5 70 IF INKEY-1:PRINT "Shift+"; 80 IF INKEY-2:PRINT "Ctrl+"; 90 IF K%=&87:PRINT "COPY"; 100 IF K%=&88:PRINT "LEFT"; 110 IF K%=&89:PRINT "RIGHT"; 120 IF K%=&8A:PRINT "DOWN"; 130 IF K%=&8B:PRINT "UP"; 140 PRINT 150 UNTIL 0[/b] The second method is to translate them to the *FX4,2 keycodes. In program startup use: [b]*FX4,2 FOR A%=8 TO 11:OSCLI "FX"+STR$(217+A%)+","+STR$(A%*16):NEXT[/b] When reading keypresses use: [b]K%=GET INKEY-256=1:A%=INSTR("[^|_`]~\}{",CHR$K%)-1:IF A%>-1:K%=&9B+(A%DIV5)*16+A%MOD5[/b] This translates modified editing keys to the standard *FX4,2 keycodes where b5=CTRL, b4=SHIFT, b3-b0=keypress, as in this demo: [b] 200 REM Electron cursor keys with *FX4,2 210 : 220 *FX4,2 230 FOR A%=8 TO 11:OSCLI "FX"+STR$(217+A%)+","+STR$(A%*16):NEXT 240 REPEAT 250 K%=GET 260 IF INKEY-256=1:A%=INSTR("[^|_`]~\}{",CHR$K%)-1:IF A%>-1:K%=&9B+(A%DIV5)*16+A%MOD5 270 IF (K% AND &F0)=&90:PRINT "Shift+"; 280 IF (K% AND &F0)=&A0:PRINT "Ctrl+"; 290 IF (K% AND &8F)=&8B:PRINT "COPY"; 300 IF (K% AND &8F)=&8C:PRINT "LEFT"; 310 IF (K% AND &8F)=&8D:PRINT "RIGHT"; 320 IF (K% AND &8F)=&8E:PRINT "DOWN"; 330 IF (K% AND &8F)=&8F:PRINT "UP"; 340 PRINT 350 UNTIL FALSE[/b]