Raw Keyboard Input

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Zetr0
Member
Posts: 59
Joined: Mon Aug 22, 2011 12:38 pm

Raw Keyboard Input

Post by Zetr0 »

Hello there my fellow z88dk'ers

I am hoping that some might know of how I can get a RAW keyboard value from stdin.

I have looked about and searched here, as such I have tried a few options -

char ch=0;

ch = getchar()
ch = fgetc()
ch = getkey()

However each one I use doesn't return the value I expect in "ch". As an example, when I Press "Enter" instead of "ch" containing a value of [ 13 ], it instead contains the value [ 10 ] - since this is the same as the value for "Cursor Key Down" - this causes quite some issue.

I am trying to keep everything in <stdio.h>

Any ideas would be great, also, I am always happy to add more assembly based functions.

Thanks for reading.
User avatar
GerardWassink
New member
Posts: 8
Joined: Sun Dec 11, 2022 10:49 am

Re: Raw Keyboard Input

Post by GerardWassink »

The value 10 (0x0a) is in fact the value for LF (Linefeed).
In the old telex world (and in DOS I might add), CRLF (0x0D followed by 0x0A) was the combo of:
- Carriage return
- Line Feed

HTH, Gerard
Last edited by GerardWassink on Mon Dec 12, 2022 10:54 am, edited 1 time in total.
Zetr0
Member
Posts: 59
Joined: Mon Aug 22, 2011 12:38 pm

Re: Raw Keyboard Input

Post by Zetr0 »

GerardWassink wrote: Mon Dec 12, 2022 10:51 am The value 10 (0x0a) is in fact the value for LF (Linefeed).
In the old telex world (and in DOS I might add), CRLF (0x0D followed by 0x0A) was the combo of:
- Carriage return
- Line Feed

HTH, Gerard
Thank you Gerard!

I suspected that it would be something of this nature, one of the super powers of Z88DK is its versatility for multi-platform support.

I have been working specifically in the ZX Spectrum camp, as such it has been my centric focus, often leading to presumption, and then reaching for the forums when it doesn't work.

I had been working from this appendix from "https://worldofspectrum.org/ZXBasicManu ... nappa.html"

I slapped this basic code that hits up a ZXSpectrum System Variable ( LAST K ) at address 23560 / 5C08h

Code: Select all

#typedef unsigned char ubyte

ubyte inkeys( void );
ubyte inkeys( void )
{
    ubyte *lastkey = 23560;      // Last Key press ZX Spectrum System Var
    ubyte ch = 0;

    //flush the last keypress
    *lastkey = 0;

    while( ch == 0 )
    {
        ch = *lastkey;
    }
    return ch;
}
This works for the intent that I need ( menu driven system ) and should I eventually get round to that arcade game I want to write, this probably will cause issues later down the line.

Thanks for your insight Gerard!
Zetr0
Member
Posts: 59
Joined: Mon Aug 22, 2011 12:38 pm

Re: Raw Keyboard Input

Post by Zetr0 »

Just to tidy that code up a bit

Code: Select all

typedef unsigned char ubyte;

ubyte getch( void );
ubyte getch( void )
{
    ubyte *LASTK = 23560;   // ZX Spectrum System Variable Address
    // fflush last input
    *LASTK = 0;
    // wait untill a keypress
    while( *LASTK == 0 );
    // return keypress value
    return *LASTK;
}
This code waits until the "LASTK" system variable is updated. As it is, this function is not really an applicable use case for say an arcade game or one that requires the user to perform any key press in regard to other timing inputs such like a rhythem game or visual pattern or movement.
Post Reply