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.



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


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"?


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


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.


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?


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



[Please log in to post a comment]