[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
Drutt
Posts: 11
Joined: Sat Mar 02, 2019 1:16 pm

[BASIC] How to read from the keyboard buffer?

Post by Fabrizio Caruso »

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
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

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

Post by Ast A. Moore »

Use the LAST K system variable maybe (23560)?
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
Drutt
Posts: 11
Joined: Sat Mar 02, 2019 1:16 pm

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

Post by Fabrizio Caruso »

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.
Fabrizio Caruso
Drutt
Posts: 11
Joined: Sat Mar 02, 2019 1:16 pm

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

Post by Fabrizio Caruso »

Maybe I could do something like:
let c=peek(23560): if c<>13 then print c: POKE 23560,13
Fabrizio Caruso
Drutt
Posts: 11
Joined: Sat Mar 02, 2019 1:16 pm

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

Post by Fabrizio Caruso »

THANKS A LOT!

I solved the problem by reading from 23560 and resetting it once I consume the input.
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

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

Post by Ast A. Moore »

;)
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