Code: Select all
key = {}
function keyisdown(v)
for i=1, #key do if v == key[i] then return true else return false end end
end
function love.draw()
if keyisdown("a") then love.graphics.print("a", 16, 16) end
if keyisdown("b") then love.graphics.print("b", 16, 32) end
if keyisdown("a") and keyisdown("b") then love.graphics.print("ab", 16, 48) end
love.graphics.print(#key, 16, 64)
end
function love.update(dt)
end
function love.keypressed(k, u)
key[#key+1] = k
end
function love.keyreleased(k, u)
for i=1, #key do if k == key[i] then table.remove(key, i) end end
end
I wrote this in one of my projects just now and it didn't work, so I rewrote it in a new program so that I could check if it was the code itself or if something else I wrote was messing it up, and since, this code alone isn't working, I'd say the problem is somewhere in here. Now I, personally don't see any reason why this doesn't work, so it'd be much appreciated if someone with greater knowledge of love/lua could assist me. The goal of this code is to be able to have multiple keys pressed at once and each one doing a different thing, and in this code for simplicity, when I press 'a' on my keyboard I want "a" printed on my screen, and "b" printed when I press 'b' and when both keys 'a' and 'b' are pressed I should see "a" "b" and "ab" printed. This works when keys are pressed separately so "a" prints when 'a' is pressed and "b" prints when 'b' is pressed, but never are all "a" "b" and "ab" printed at the same time. I don't know why this doesn't work for me, so if there's a problem I don't see or there's a better more logical way to approach this, again it'd be much appreciated for the help.