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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

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

Post by Inny »

Let me take a stab:

Code: Select all

function makeKeyCombo( ... )
  return function()
    for i = 1, select('#', ...) do
      if not love.keyboard.isDown(select(i, ...)) then return false end
    end
    return true
  end
end

ctrld = makeKeyCombo( "lctrl", "d" )

function love.update(dt)
  if ctrld() then print("Nice.") end
end
Just an idea, but probably not very good as the lctrl and rctrl difference might be a problem.

EDIT: Oops, the vararg can't be captured by the closure, so that code won't work.

Use this instead, maybe:

Code: Select all

function makeKeyCombo( ... )
  local keys = {...}
  return function()
    for i = 1, #keys do
      if not love.keyboard.isDown(keys[i]) then return false end
    end
    return true
  end
end
Last edited by Inny on Tue Mar 06, 2012 5:34 am, edited 1 time in total.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

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

Post by Xgoff »

if you wanted to get really ridiculous you could implement something like

Code: Select all

if Key "ctrl" + Key "space" then end -- overengineered way of 'ctrl down and space down'
if Key "ctrl" - Key "space" then end -- ditto for 'ctrl down and not space down'
if Key "ctrl" / Key "space" then end -- ditto for 'ctrl down or space down'
if Key "ctrl" ^ Key "space" then end -- slightly more useful (in the few cases you'd ever need it i guess), 'ctrl down xor space down'
if Key "ctrl" < Key "space" then end -- 'ctrl down and space down, but ctrl was pressed first'
if #Key "ctrl" >= 5 then end --'ctrl held down for 5 or more time units (whatever you're using for those)'
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld »

Immediate mode is superior again:

Code: Select all

function love.keyboard.allDown(k, ...)
    if not k then return true end
    return love.keyboard.isDown(k) and love.keyboard.allDown(...)
end
Usage:

Code: Select all

if love.keyboard.allDown("lctrl", "d") then foo() end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
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 »

vrld wrote:Immediate mode is superior again:

Code: Select all

function love.keyboard.allDown(k, ...)
    if not k then return true end
    return love.keyboard.isDown(k) and love.keyboard.allDown(...)
end
Should that not be return false?

Either way I love this method.
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

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

Post by Ellohir »

When the list is empty you have to return true, so that it doesn't break all the ands made with the keys.
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 »

Ohh, yeah, sorry. That makes more sense. Sometimes, once maybe every few years, I'll do something stupid. You should consider yourself lucky you expirienced one of these... 'rare'... events.
>.>
<.<
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests