Boriel Basic Forum
reading simultaneous keys pressed - Printable Version

+- Boriel Basic Forum (https://forum.boriel.com)
+-- Forum: Compilers and Computer Languages (https://forum.boriel.com/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://forum.boriel.com/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://forum.boriel.com/forumdisplay.php?fid=16)
+---- Thread: reading simultaneous keys pressed (/showthread.php?tid=385)



reading simultaneous keys pressed - nitrofurano - 2011-10-08

i forgot how to read simultaneous keys pressed on zx-spectrum - is it from port 254? and only port 254? and how?
and how can we read them in a zxbasic-compiler snippet?
thanks! Smile


Re: reading simultaneous keys pressed - boriel - 2011-10-09

Use the multikeys function in the library which returns non zero (True) if ANY of the given keys is pressed.
Code:
#include <keys.bas> DO IF MultiKeys(KEYSPACE | KEYENTER) THEN PRINT "Space or Enter pressed" END IF LOOP : REM Repeat forever since no condition specified
Predefined key constants are in the KEY.BAS file. Read there.

Notice you can use | as in C or bOR to test for multiple values simultaneously.
So the above test checks for ENTER or SPACE pressed.
The function is much faster than INKEY$ and can check multiple keys pressing (limited by the ZX hardware)


Re: reading simultaneous keys pressed - nitrofurano - 2011-10-09

it doens't work... - are you sure this snippet and the used library are working fine?
the code (in the spectrum) looks like hanging - it doesn't provide any feedback

btw, has ./library/keys.bas 3535 bytes, 147 lines, and the md5sum is 8eb7625442d3c108ee6b90ded47aff8c?

Code:
guest@macbook_mint1 /mnt/sda4/trabalhos/programacao/8bit/zxspectrum/ftpupload/zxspectrumstuff_20111001/borielzxbasiccompiler/_sandboxes/_sandbox_03 $ zxb.py -tB a _keytest.bas Generating LALR tables WARNING: Token 'BIN' defined, but not used WARNING: There is 1 unused token Generating LALR tables WARNING: Token 'UMINUS' defined, but not used WARNING: There is 1 unused token guest@macbook_mint1 /mnt/sda4/trabalhos/programacao/8bit/zxspectrum/ftpupload/zxspectrumstuff_20111001/borielzxbasiccompiler/_sandboxes/_sandbox_03 $ zxb.py --v ersion Generating LALR tables WARNING: Token 'BIN' defined, but not used WARNING: There is 1 unused token Generating LALR tables WARNING: Token 'UMINUS' defined, but not used WARNING: There is 1 unused token zxb.py 1.2.8-s758 guest@macbook_mint1 /mnt/sda4/trabalhos/programacao/8bit/zxspectrum/ftpupload/zxspectrumstuff_20111001/borielzxbasiccompiler/_sandboxes/_sandbox_03 $



Re: reading simultaneous keys pressed - boriel - 2011-10-09

You have to execute zxb.py --version in the same directory zxb.py is installed, and must have write permissions on such directory.

The snippet is not working, that's right, because I forgot you have to check simultaneously KEYS in the same semirow (look at the ZX Spectrum keyboard layout).
E.g. The following will work:
Code:
#include <keys.bas> DO IF MultiKeys(KEYA| KEYS) THEN PRINT AT 0, 0; "Letter A or letter S pressed (or both)" ELSE PRINT AT 0, 0,, END IF LOOP : REM Repeat forever since no condition specified
To check ANY key use
MultiKeys(<key 1>) AND MultiKeys(<Key 2>)


Re: reading simultaneous keys pressed - nitrofurano - 2011-10-09

btw, i started trying these snippets: (and found how to get keyboard values directly from 'in' command)

this works, but i'm not very fan of this complexity of values, which makes me struggling when using similar stuff with games with redefinable keys (like those from Dinamic and so on)
Code:
10 print at 0,0;" " 20 print (255-(in 65278)) band 31;" " 21 print (255-(in 65022)) band 31;" " 22 print (255-(in 64510)) band 31;" " 23 print (255-(in 63486)) band 31;" " 24 print (255-(in 61438)) band 31;" " 25 print (255-(in 57342)) band 31;" " 26 print (255-(in 49150)) band 31;" " 27 print (255-(in 32766)) band 31;" " 30 goto 10

i wanted to do this (but the code doesn't works...)
Code:
function getkey(kv as ubyte) as ubyte dim v1,v2,v4 as ubyte dim v3 as uinteger v1=kv band 31 v2=(kv band 224)/32 v3=254+256*(255-2^v2) v4=((in v3) band v1)=0 return v4 end function main: print at 0,0;" " print getkey(3) goto main

and when i tried to figure out what were going wrong, this code hangs (like 'usr 0')
Code:
dim v3 as uinteger dim i,v5 as ubyte main: for i=0 to 7 print at 0,i;i;"-"; v3=254+256*(255-2^i):print v3;" "; v5=31-((in v3) band 31):print v5;" "; next i goto main



Re: reading simultaneous keys pressed - boriel - 2011-10-09

It seems we crospost. Let me repeat here:

The snippet is not working, that's right, because I forgot you have to check simultaneously KEYS in the same semirow (look at the ZX Spectrum keyboard layout).
E.g. The following will work:
Code:
#include <keys.bas> DO IF MultiKeys(KEYA| KEYS) THEN : REM 'A' and 'S' are in the same semirow PRINT AT 0, 0; "Letter A or letter S pressed (or both)" ELSE PRINT AT 0, 0,, END IF LOOP : REM Repeat forever since no condition specified
To check ANY key use MultiKeys(<key 1>) AND MultiKeys(<Key 2>) so you don't have to care about rows, positions, etc...


Re: reading simultaneous keys pressed - nitrofurano - 2011-10-09

yes, it works now, but i'm really looking for simultaneous keys pressed from all semirows, like from this example:

the code below works, it reads from both cursors and 'wsad' keys
Code:
cls main: print at 0,0;" " v1=((255-(in 64510)) band 2)/2 bor ((255-(in 65022)) band 2) bor ((255-(in 65022)) band 1)*4 bor ((255-(in 65022)) band 4)*2 v2=((255-(in 61438)) band 8)/8 bor ((255-(in 61438)) band 16)/8 bor ((255-(in 63486)) band 16)/4 bor ((255-(in 61438)) band 4)*2 v3=v1 bor v2 print v3;" " goto main