I think I posted this module on the forums in order to help someone a few weeks ago, but I thought It'd get more use if I created a blog post so it doesn't get diluted down in the forums.
I've written this event module to make it easy to add keyboard and mouse button binds and it's come in handy to me for quite a few little scripts.
Once again excuse the poor spacing as copy and paste here seems to remove my tabs.
event.lua
-- Event module written by Averice
event = {}
event.inputBinds = {}
event.mouseBinds = {}
function event.CheckInput()
local succ, err;
for key,tab in pairs(event.inputBinds) do
if( App.window:KeyHit(key) ) then
for k, v in pairs(tab) do
if( not v.Held ) then
succ, err = pcall(v.Func, unpack(v.Args));
if not succ then
print("Event Error[Key]["..k.."]: "..err);
end
end
end
end
if( App.window:KeyDown(key) ) then
for k, v in pairs(tab) do
if( v.Held ) then
succ, err = pcall(v.Func, unpack(v.Args));
if not succ then
print("Event Error[Key]["..k.."]: "..err);
end
end
end
end
end
for but, tab in pairs(event.mouseBinds) do
if( App.window:MouseDown(but) ) then
for k, v in pairs(tab) do
succ, err = pcall(v.Func, unpack(v.Args));
if not succ then
print("Event Error[Mouse]["..k.."]: "..err);
end
end
end
end
end
function event.AddInputBind(key, name, held, func, ...)
local newInput = {
Func = func,
Args = {...},
Held = held or false
}
event.inputBinds[key] = event.inputBinds[key] or {};
event.inputBinds[key][name] = newInput;
end
function event.AddMouseBind(but, name, func, ...)
local newInput = {
Func = func,
Args = {...}
}
event.mouseBinds[but] = event.mouseBinds[but] or {}
event.mouseBinds[but][name] = newInput;
end
function event.RemoveInputBind(key, name)
if( event.inputBinds[key] and event.inputBinds[key][name] ) then
event.inputBinds[key][name] = nil;
end
end
function event.RemoveMouseBind(but, name)
if( event.mouseBinds[but] and event.mouseBinds[but][name] ) then
event.mouseBinds[but][name] = nil;
end
end
It's usage is really straight forward, you import the script, and in your App.loop you'll put
event.CheckInput();
This will check for your binds and run them if needed so you don't have to fill your App.loop with key binds.
To add a keyboard bind "event.AddInputBind(key, name, held, function, args);
local function printStuff(...)
print("Hello", ...);
end
event.AddInputBind(Key.A, "RandomName", false, printStuff, "How", "are", "you") -- varargs once again.
--when the A key is pushed it should output, "Hello How are you";
-- if 'held' is true it will keep printing while it's held.
--To remove it we use it's name and key.
event.RemoveInputBind(Key.A, "RandomName");
-- Mouse bind functions are the same, just use event.AddMouseBind and event.RemoveMouseBind
-- mouse bind functions are considered always held. Add a check to your function if you only
-- want it done per click.
A quick snippet that I use in my splash screen with my StateManager module to cancel the splash if escape is pushed.
event.AddInputBind(Key.Escape, "__SHARDSPLASH", false, function() StateManager:Pop() end);
-
3
5 Comments
Recommended Comments