Log In  

So, as shown in my previous two posts, I'm making an operating system for Pico-8 called P8OS.
I am making it so that Z, closest to the window/whatever key, activates the app menu, and X, right next to it activates the task menu, where you can see which windows/programs are active. I need to check for both of them every frame. Shortened down to the problematic code, here is where it goes wrong:

function btnk(k)
  local p=stat(30)and stat(31)
  if k!=nil then
    if(p==k)return true
    if(p!=k)return false
  else
    return p
  end
end
if(btnk('z'))inmenu=not inmenu
if(btnk('x'))intask=not intask

It will not detect pressing X unless Z is pressed in the same frame, which is even more confusing.
Clearly something has gone desperately wrong somewhere in this code. I just can't pinpoint what.

P.S. Keep in mind that if you want to reproduce this you will need to call poke(24365,1) or equivalents at the start of your code.

P#88683 2021-03-08 18:41 ( Edited 2021-03-12 16:37)

you need to store keyboard events in a table to reuse them

P#88687 2021-03-08 19:26

Lets say the buffer stores "XZ", but you run:

btnk("Z")
btnk("X")

Won't they both return false, since it's stripping "X" off the buffer first and comparing it to "Z", then stripping "Z" off the buffer and comparing it to "X"?

P#88688 2021-03-08 19:34

Yeah, that is a reason to why the problem is happening. Thanks for letting me know. But I still don't see an obvious solution, which is the reason I asked the question

P#88689 2021-03-08 19:45

Nevermind. Came up with a solution. Basically, you just pop the buffer once at the beginning of every frame and use that instance to check.

P#88690 2021-03-08 19:53

You wouldn't want to pop just once, though, right? Wouldn't you want the whole buffer? Something along the lines of:

local buffer=""
while (stat(30)) do
    buffer..=stat(31)
end

(Or add to a table instead of a string, like @freds72 suggested.) And then you can just run through all the keystrokes that were in the buffer from that frame?

P#88692 2021-03-08 20:38

Ah. Yeah, haven't thought of that. Thanks!

P#88737 2021-03-09 18:46

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 08:29:37 | 0.014s | Q:19