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

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.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

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

Post 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
Last edited by Prof_Ants on Mon Mar 05, 2012 11:10 am, edited 1 time in total.
User avatar
SimonLarsen
Party member
Posts: 100
Joined: Thu Mar 31, 2011 4:47 pm
Location: Denmark
Contact:

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

Post 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.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

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

Post by Prof_Ants »

What I am aiming to do is declare a key press combination. Will the comma separate statement produce this affect?
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

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

Post by IMP1 »

Code: Select all

if love.keyboard.isDown("lctrl") and love.keyboard.isDown("d") then
    -- speed = 100 or whatever
end
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

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

Post by Prof_Ants »

Cheers mate!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

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

Post 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.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Shit, you're right.

Forget what I said. :P
Help us help you: attach a .love.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

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

Post 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
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

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

Post 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!
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests