Page 1 of 2

Declare a key press combination for love.keyboard.isDown?

Posted: Mon Mar 05, 2012 10:57 am
by Prof_Ants
I can't seem to find it detailed in the wiki, but am I able to declare a love.keyboard.isDown with multiple keys?

I tried this (to no avail):

Code: Select all

if love.keyboard.isDown("lctrl" + "d") then
            speed = 100
       end

Re: Can I declare multiple keys for love.keyboard.isDown?

Posted: Mon Mar 05, 2012 11:03 am
by SimonLarsen
I think the wiki makes it pretty clear that you can give it multiple comma separated keys:

Code: Select all

Synopsis
   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.

Re: Can I declare multiple keys for love.keyboard.isDown?

Posted: Mon Mar 05, 2012 11:06 am
by Prof_Ants
What I am aiming to do is declare a key press combination. Will the comma separate statement produce this affect?

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 11:20 am
by IMP1

Code: Select all

if love.keyboard.isDown("lctrl") and love.keyboard.isDown("d") then
    -- speed = 100 or whatever
end

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 11:23 am
by Prof_Ants
Cheers mate!

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 12:12 pm
by Robin
Why your initial idea doesn't work is because of this:

Code: Select all

if love.keyboard.isDown("lctrl" + "d") then
Let's look at that argument:

Code: Select all

"lctrl" + "d"
That is the same as writing

Code: Select all

"lctrld"
Since there is no key named lctrld, it can never be down, and the call always returns false.

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 1:15 pm
by tentus
Robin wrote:Why your initial idea doesn't work is because of this:

Code: Select all

if love.keyboard.isDown("lctrl" + "d") then
Let's look at that argument:

Code: Select all

"lctrl" + "d"
That is the same as writing

Code: Select all

"lctrld"
Since there is no key named lctrld, it can never be down, and the call always returns false.
(Robin, we're not using Javascript right now!)

Actually, arithmetic on strings isn't supported in Lua. To concatenate strings ("lctrl" + "d" = "lctrld") you need to use the .. operator ("lctrl" .. "d" = "lctrld"). The above fails because you can't add words together.

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 2:45 pm
by Robin
Shit, you're right.

Forget what I said. :P

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 8:35 pm
by T-Bone
Another thing that might be good to keep in mind:

Often when you have key combinations in many applications, you typically hold one key down and then click the other one (like Ctrl + C). A good way to implement this is

Code: Select all

function love.keypressed(k)
    if k == "c" and love.keyboard.isDown("ctrl") then
        --stuff
    end
end

Re: Declare a key press combination for love.keyboard.isDown

Posted: Mon Mar 05, 2012 9:47 pm
by tentus
T-Bone wrote:

Code: Select all

if k == "c" and love.keyboard.isDown("ctrl") then
Wow, somehow it had never occurred to me to do it that way- good tip!