[BASIC] How to read from the keyboard buffer?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
Fabrizio Caruso
Berk
Posts: 6
Joined: Sat Mar 02, 2019 1:16 pm

[BASIC] How to read from the keyboard buffer?

Post by Fabrizio Caruso » Sat Mar 23, 2019 8:33 pm

Hi!

I am struggling with the input in the Spectrum version of my BASIC game "Mines":
https://github.com/Fabrizio-Caruso/Sinc ... -10-liners

I need to read the last key-pressed in a loop without stopping if no key is pressed.
I don't want to miss key-presses.
I currently use "LET S = CODE INKEY$" but this works randomly because
it seems not to read from the keyboard buffer. It seems it is only reading the current keyboard status.
So if by chance the key was pressed and released just before, then the last key pressed is lost.

What is the correct way to read the keyboard in a "buffered way"?

Fabrizio
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1224
Joined: Mon Nov 13, 2017 3:16 pm

Re: [BASIC] How to read from the keyboard buffer?

Post by Ast A. Moore » Sat Mar 23, 2019 8:51 pm

Use the LAST K system variable maybe (23560)?
1 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

Fabrizio Caruso
Berk
Posts: 6
Joined: Sat Mar 02, 2019 1:16 pm

Re: [BASIC] How to read from the keyboard buffer?

Post by Fabrizio Caruso » Sat Mar 23, 2019 11:47 pm

How do I detect no ket pressed with 23560.
I want to get the last key pressed only once and not forever.
It is for an interactive non-stopping action game.

So if a key is pressed I need to detect it and not miss it but I need to detect it just once.

In principle INKEY seems to do that but it does not in my case.
It misses many key-presses. I suppose it fails to detect keys pressed before INKEY$ execution because
they are not buffered.
0 x

Fabrizio Caruso
Berk
Posts: 6
Joined: Sat Mar 02, 2019 1:16 pm

Re: [BASIC] How to read from the keyboard buffer?

Post by Fabrizio Caruso » Sat Mar 23, 2019 11:56 pm

Maybe I could do something like:
let c=peek(23560): if c<>13 then print c: POKE 23560,13
0 x

Fabrizio Caruso
Berk
Posts: 6
Joined: Sat Mar 02, 2019 1:16 pm

Re: [BASIC] How to read from the keyboard buffer?

Post by Fabrizio Caruso » Sun Mar 24, 2019 12:46 am

THANKS A LOT!

I solved the problem by reading from 23560 and resetting it once I consume the input.
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1224
Joined: Mon Nov 13, 2017 3:16 pm

Re: [BASIC] How to read from the keyboard buffer?

Post by Ast A. Moore » Sun Mar 24, 2019 8:10 am

;)
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

Post Reply