No UN-DEFINEKEY? ... INKEY ... PEN

Started by lettersquash, March 15, 2020, 17:06:21

Previous topic - Next topic

lettersquash

Say I want to interrupt a loop with a key press, diverting to some other routine, which is possible with something like
DEFINEKEY ASC("p"), mySub
in the initialisation section. Now, when I tap the 'p' key, it diverts to mySub as expected.

I can exit the subroutine in the usual ways. I can PAUSE (without argument) and hit another key to continue to END (the sub), but I can't hit 'p', or it diverts immediately to the sub again.  It would be neat if I could return to the main loop by a second press of 'p'. Even more of a problem is that no press of 'p' doesn't get grabbed by the interrupt, so you can't pause the loop and ask for input, or if you do and enter a 'p', you're back to the start of the subroutine.

What's needed is an UNDEFINEKEY asc("p"), or DEFINEKEY asc("p"), ""
but those don't seem to work.

I've tried a few possible solutions, including starting the subroutine with a new definition to a dummy subroutine, but it seems to be ignored (although you can define other keys inside a sub). It seems once defined, that's it, it stays defined.

I thought I had it when I made the pause routine just toggle a variable, running, testing for running=0 in the main loop and diverting to a second subroutine for the inputs, etc. This avoids the immediate interruption of input, but control appears still to be diverting to the toggle subroutine each time a p is pressed, so 2 (or any even number of) presses and when I finish input, it pauses again, any odd number in the input string and running is true again, so it's still not a solution.

Any thoughts...apart from maybe just using single key presses to interrupt the loop that aren't going to be used elsewhere, but there's not many to choose from. I can pause with a backspace, say, and exit the mySub with ESC.

REM SmallBASIC
REM created: 15/03/2020

definekey asc("p"), myPause

i=0
repeat
  i++
  locate 1,1:? i;" ";:showpage:delay 1000
until 0

sub myPause
  definekey asc("p"), release
  locate 4,0
  input "Type something without a 'p':",inp
  input "Type something with a 'p':",inp
  ? "See the problem?"
end sub

sub release
  beep
  ? "Doesn't get here."
  throw
end sub
I'll have you know, I'm coding all the right commands, just not necessarily in the right order.

lettersquash

Ah, no, I can't use backspace. Probably a mouse button's best.  ;D
I'll have you know, I'm coding all the right commands, just not necessarily in the right order.

bplus

#2
I wouldn't expect the definekey thing to work inside INPUT, maybe...

Here is myPause that works great! with definekey

REM SmallBASIC
REM created: 15/03/2020

definekey asc("p"), myPause

i=0
repeat
  cls
  ? "Secs since midnight = ";timer
  ?
  ? "Press p to check myPause sub..."
  showpage
until asc(inkey) = 27

sub myPause
  locate 5, 1: print "And now let us pause for a moment (=5 secs) of silence."
  showpage  '<<<<<<<<<< once you use showpage you always have to use it to show anything
  moment = 5
  tstart = timer
  while timer < tstart + moment : wend
end sub



1 person likes this

lettersquash

Yes, I forgot to showpage in my SUB release! Now it shows some even more schizophrenic behaviour!

But I've realised another mistake I made. I thought I'd discovered that INKEY stops the program and waits for a keypress, which was why I switched to DEFINEKEY. I have just tried INKEY again and it doesn't stop, it works as I originally expected (as it does in BBC BASIC, for example), so that's all I needed in the first place! Maybe there are different ways to use it, or maybe I just wrote something buggy. Cheers.

That's easy then, I can just inkey out of the main loop and inkey to exit sub, even with the same key if I want, and inputs won't cause interrupts.
I'll have you know, I'm coding all the right commands, just not necessarily in the right order.

lettersquash

You're such a great help bplus. I'm learning a lot here!

Again, just if anyone's searching and has similar issues, and remembering that I'm just using smallBASIC to write programs to run on Windows (although they should work on Android):
INKEY statements do appear sometimes to stop my program progressing. I think it might be a timing or buffer overflow problem.

I have a REPEAT ... UNTIL 0 with a FOR ... NEXT loop inside it, and the inner loop is going at insane speeds (well, they both are). :P  If I check for a keypress in the inner loop, the program freezes. If I do it outside the inner loop, it does as the help file says it should, it checks for the keypress and reacts if found, but continues round the loop otherwise. That is using:
IF INKEY="p" then doStuff

Unfortunately, it's comparatively slow, but a much faster test is PEN. For example, I'm now testing for a LMB click:
IF PEN(3) THEN myPause
which hardly affects the speed at all and works in either of the loops. 8) I'll probably add coordinates checks so I can click on 'buttons'.
I'll have you know, I'm coding all the right commands, just not necessarily in the right order.

bplus

Hey LS, I think you are right, INKEY can bog down a great graphics program and definekey might just get around that.
1 person likes this

chrisws


lettersquash

Quote from: chrisws on March 24, 2020, 22:09:00
Good suggestion - I've created an issue so I don't forget :)

https://github.com/smallbasic/SmallBASIC/issues/92
Thanks Chris. You're a star. I'm very glad it's a good idea.
I'll have you know, I'm coding all the right commands, just not necessarily in the right order.