Reading shift key status from BASIC

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Reading shift key status from BASIC

Post by Adam James » Tue Jul 07, 2020 8:43 pm

Is it possible to read the status of the left and right shift key from BASIC?

I guess the reason it can't be done with INKEY is that 'shift' doesn't have an ASCII code and I guess presses to the shift key do not get put in the keyboard buffer?

Also, are there any pitfalls in using the shift key as a game key in BASIC? I guess if it's not buffered and the BASIC program has a slow loop, it may not be detected if it's tapped very quickly.

I'd really like to have two players both with their own shift key and to be able to reliably detect separate or simultaneous presses if that's possible.

Thanks in quivering anticipation...

User avatar
hoglet
Posts: 9472
Joined: Sat Oct 13, 2012 7:21 pm
Location: Bristol
Contact:

Re: Reading shift key status from BASIC

Post by hoglet » Tue Jul 07, 2020 8:51 pm

Adam James wrote:
Tue Jul 07, 2020 8:43 pm
Thanks in quivering anticipation...
The shift keys are connected in parallel in the keyboard matrix; there is no way to read them seperately.

Naomasa298
Posts: 391
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Re: Reading shift key status from BASIC

Post by Naomasa298 » Tue Jul 07, 2020 8:57 pm

INKEY(-1) will read the status of the shift key, but that will return true if either shift key is pressed. I don't know about reading the left or right one separately.

Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Re: Reading shift key status from BASIC

Post by Adam James » Tue Jul 07, 2020 10:24 pm

hoglet wrote:
Tue Jul 07, 2020 8:51 pm
The shift keys are connected in parallel in the keyboard matrix; there is no way to read them seperately.
Drat. Good to know though, thank you:)
Naomasa298 wrote:
Tue Jul 07, 2020 8:57 pm
INKEY(-1) will read the status of the shift key
Great, thanks.

For a moment I started to think that it didn't matter because I'd know, from what was going on in the game at the time, that a shift key must be being pressed by a certain player, but no, there would be plenty of scope for one player to deliberately interfere with another!

Oh, well:) That's saved me going down a blind alley, thanks guys.

julie_m
Posts: 237
Joined: Wed Jul 24, 2019 9:53 pm
Location: Derby, UK
Contact:

Re: Reading shift key status from BASIC

Post by julie_m » Tue Jul 07, 2020 11:28 pm

Note that negative parameters to INKEY can be used to check the status of any individual key; there is a table in the User Guide. If you use a FOR loop something like this:

Code: Select all

100K%=0
110REPEAT
120FORI%=-1TO-127STEP-1
130IFINKEYI%K%=I%:I%=-127:PRINT"INKEY(";K%;")=-1"
140NEXTI%
150UNTILK%
you have the basis of a "select your own direction keys" feature.

Adam James
Posts: 196
Joined: Tue May 26, 2020 2:32 pm
Contact:

Re: Reading shift key status from BASIC

Post by Adam James » Wed Jul 08, 2020 11:47 am

julie_m wrote:
Tue Jul 07, 2020 11:28 pm
Note that negative parameters to INKEY can be used to check the status of any individual key; there is a table in the User Guide. If you use a FOR loop something like this ... you have the basis of a "select your own direction keys" feature.
Top tip, thanks:) That's a nice terse way of implementing a redefine-keys routine.

Right now this game has only one key per player, it's all about timing, but I'll remember the above:)

Post Reply

Return to “programming”