Multiple key press function thing not working

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Chevelle1995
Prole
Posts: 1
Joined: Wed Jun 24, 2015 1:12 am

Multiple key press function thing not working

Post by Chevelle1995 »

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.
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multiple key press function thing not working

Post by zorg »

A simple question, why don't you use [wiki]love.keyboard.isDown[/wiki] instead of writing your own?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple key press function thing not working

Post by ivan »

Hello. I think there are easier/simpler ways to accomplish what you're going for.
For example:

Code: Select all

function love.keyboard.areDown(k1, k2, ...)
  if not love.keyboard.isDown(k1) then
    return false
  end
  if k2 then
    return love.keyboard.areDown(k2, ...)
  end
  return true
end
Usage:

Code: Select all

if love.keyboard.areDown('a', 'b') then
Note that this function tries to returns false as soon as possible, rather than checking every supplied key.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: Multiple key press function thing not working

Post by pielago »

?? like how??

Code: Select all

function love.load()
love.graphics.setBackgroundColor(50,50,50)
k1="A"
k2="S"
end
pressing both at same time???

Code: Select all

function love.draw()
	local down =love.keyboard.isDown
	local print=love.graphics.print
	
	if down("a") and down("s") then
		love.graphics.setColor(200,200,200)
		print(k1..k2,10,10)
	elseif down("a") then
		love.graphics.setColor(200,200,0)
		print(k1,10,10)
	elseif down("s") then
		love.graphics.setColor(200,0,200)
		print(k2,10,10)
	end

end

love.keyboard.areDown(k1, k2, ...)
how does that one works?
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple key press function thing not working

Post by ivan »

how does that one works?
You have to test it, but it should return true when all of the supplied keys are pressed at the same time:

Code: Select all

	if love.keyboard.areDown("a", "s") then
		love.graphics.setColor(200,200,200)
and you can supply more than two keys:

Code: Select all

	if love.keyboard.areDown("a", "s", "w", "d") then
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: Multiple key press function thing not working

Post by Ortimh »

Maybe this one as alternative?

Code: Select all

function love.keyboard.areDown(...)
	for index, key in pairs({...}) do
		if (not love.keyboard.isDown(key)) then
			return false
		end
	end
	
	return true
end
Basically it does the same as ivan's.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: Multiple key press function thing not working

Post by pielago »

you know I Actually never understood the (...)

Code: Select all

    function love.keyboard.areDown(...)
       for index, key in pairs({...}) do
          if (not love.keyboard.isDown(key)) then
             return false
          end
       end
       
       return true
    end
but anyways.
I made mine as simple as possible under the update will it fail?
I mean all he wants its both at same time.. Right?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Multiple key press function thing not working

Post by Positive07 »

ivan wrote:-snip-
I have an alternative too

Code: Select all

love.keyboard.areDown = love.keyboard.isDown
Wiki wrote: Synopsis

Code: Select all

anyDown = love.keyboard.isDown( key1, key2, key3, ... )
Arguments
KeyConstant keyN = A key to check.
Returns
boolean anyDown = True if any supplied key is down, false if not.
Here is the page!

EDIT: Oh! I just noticed you want the oposite effect, true if all keys are down... Then yeah I would do something like:

Code: Select all

love.keyboard.areDown = function (...)
    for i=1, select("#",...) do
        if not love.keyboard.isDown(select(i,...)) then return false end
    end
    return true
end
A for loop is way faster than recursion in LuaJIT, select is really optimized too, we don't create tables here so less memory consumption, and pairs is slow and has got erratic behavior

Any way this is just optimization (The root of all evil)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Multiple key press function thing not working

Post by ivan »

Positive07 wrote:A for loop is way faster than recursion in LuaJIT, select is really optimized too,
Hi. Really? Recursion is usually faster than 'select' in my experience at least with non-JIT lua.
In the worst case scenario, you have to call 'select' for each argument.
we don't create tables here so less memory consumption, and pairs is slow and has got erratic behavior
Creating an intermediate table is fast too. In fact for a large number of args, it may be significantly faster than recursion/select.
But ya, the slowdown with '{ ... }' is probably delayed until the GC step.

In any case, it seems that functions accepting a variable number of arguments are more about convenience than speed.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Multiple key press function thing not working

Post by Positive07 »

ivan wrote:
Positive07 wrote:A for loop is way faster than recursion in LuaJIT, select is really optimized too,
Hi. Really? Recursion is usually faster than 'select' in my experience at least with non-JIT lua.
In the worst case scenario, you have to call 'select' for each argument.
we don't create tables here so less memory consumption, and pairs is slow and has got erratic behavior
Creating an intermediate table is fast too. In fact for a large number of args, it may be significantly faster than recursion/select.
But ya, the slowdown with '{ ... }' is probably delayed until the GC step.

In any case, it seems that functions accepting a variable number of arguments are more about convenience than speed.
1- Yeah, for loops are really optimized in JIT and LPGhatguy (I think it was him or Shakesoda) run some tests on this and it gets really optimized because select("#",...) is turned into a number, and then the loop becomes a simple loop so there is a lot of speed up there.

2- Well that doesnt apply to JIT it seems, you should be really careful with creating tables, they allocate a bunch of memory, and they take some time to be destroyed by the GC, so as you said, the slowdown appears there... Plus there is an uncertainity on the number of arguments so Lua (in general) has to check each argument before adding it to the table... pretty pointless because all indexes are numerical... but Lua doesnt know that just yet!

Ugh optimization is such an ugly thing hahaha
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: 101Corp and 2 guests