EZControls allows you to bind keys to binding objects and add onPress and onRelease callbacks to said objects. It sports support for separate bindings for different gamestates as well as built in utilities to help load and save keybinds to files in a human readable format.
By default, it checks if the love key events are defined (love.keypressed(), etc) and defines them if they aren't. Functions for firing control events are provided in case they're defined.
local controls = require('EZControls') -- Load the library.
controls.currentState = 'game' -- Set gamestate to "game". This will only allow bindings in the gamestate "game" to work. There's a special gamestate called "all" that can contain bindings that work regardless of current gamestate.
-- Bind some keys.
controls.state('game').binding('shoot'):bind(' ') -- Bind space to binding "shoot".
controls.state('game').binding('jetpack_fire'):bind({'up', 'w'}) -- Bind up and w to binding "jetpack_fire"
-- Traditional isDown check.
function love.update(deltaTime)
if controls.state('game').binding('shoot'):isDown() then
-- shoot gun logic
end
end
-- Callback style.
controls.state('game').binding('jetpack_fire'):onPress(function() -- Pass anonymous function as an argument to onPress function.
enableJetpack()
end)
controls.state('game').binding('jetpack_fire'):onRelease(function()
disableJetpack()
end)
local humanReadableKeybinds = controls.serialize() -- Dump keybinds to table.
controls.parse(humanReadableKeybinds) -- Load keybinds from table.
New Release: v0.4.0
Added a new way to bind keys. You can now call a binding's :bindnext() method to bind the next key pressed to the binding. The method accepts a function callback as the argument. The function callback is called with the key pressed passed as an argument.
-- Load the library.
local controls = require('EZControls')
-- Set the controls library state.
controls.currentState = 'game'
-- Binds the next key pressed to the binding "jetpack_fire".
controls.state('game').binding('jetpack_fire'):bindNext(function(key)
print('binded ' .. key .. ' to binding "jetpack_fire"')
end)
-- Create callbacks for the bindings.
controls.state('game').binding('jetpack_fire'):onPress(function()
enableJetpack()
end)
controls.state('game').binding('jetpack_fire'):onRelease(function()
disableJetpack()
end)