Read keypress status of multiple keys on CX16?

Tutorials and help articles.

(Posts require approval. See pinned post.)
Forum rules
Post guides, tutorials, and other instructional content here.

This topic area requires approval, so please be patient while we review content to make sure it fits the expectations for this topic area.

Tech support questions should be asked in Hardware or Software support.
Post Reply
mwiedmann
Posts: 70
Joined: Fri Jan 20, 2023 2:21 am
Location: New York City

Read keypress status of multiple keys on CX16?

Post by mwiedmann »

Is there a way to read the keypress status of multiple keys on the CX16? I need to read the status of multiple keys at the same time. Meaning for instance, I want to know if they are pressing W, E, and R. Or know that they are NOT pressing R.

The kernal functions seem to use a buffer.
kelli217
Posts: 531
Joined: Sun Jul 05, 2020 11:27 pm

Re: Read keypress status of multiple keys on CX16?

Post by kelli217 »

It’s possible to communicate directly with the SMC, and poll the keyboard yourself instead of it being done by the kernal interrupt handler.
User avatar
Daedalus
Posts: 229
Joined: Fri Nov 11, 2022 3:03 am

Re: Read keypress status of multiple keys on CX16?

Post by Daedalus »

You can, but it's not simple. The docs for it are on the github under "Editor", the section you want is near the end and is called "Custom Keyboard Keynum Code Handler"

First, if you're using the emulator, you need to be using r43.

Then you need to be running a custom interrupt handler. Then you hook into the scan code hook at #32e/$32f (This is all in the docs, along with a sample keyboard handler to get you started.)

Note that to do what you want, you need to set a state for all the keys you want tracked independently of the keyboard based on their scan codes (What the PS/2 keyboards sends to identify each key.) When a "key down" code comes in, set the state as down, then when the "key up" code comes in, set it as released. Once that is running, your game uses that array of states to know if a key (Or many keys) are pressed at any one time.

A detail to understand is that the "key repeat" comes from the PS/2 keyboard itself, in the scan codes, not from the KERNAL. So you can receive many "key down" events on a key if it's being held down, and only one "key up" event when it's released.
Post Reply