Page 2 of 2

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

Posted: Mon Mar 05, 2012 11:43 pm
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

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

Posted: Tue Mar 06, 2012 1:50 am
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)'

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

Posted: Wed Mar 07, 2012 10:05 am
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

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

Posted: Wed Mar 07, 2012 11:10 am
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.

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

Posted: Wed Mar 07, 2012 11:44 am
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.

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

Posted: Wed Mar 07, 2012 10:21 pm
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.
>.>
<.<