BindHelper - ContextActionService handler

BindHelper is a tiny module that holds and handles Bind Actions from ContextActionService. It can be used to store the actions and re-use them in other scripts (if you wish to have keybinds in your game that are set to a system), and whatever more you can think of.

Here is an example of how it works:

local BindHelper = require(path.to.BindHelper)
local KeybindStorage = BindHelper.new("KeybindStorage",  -- The name of the BindHelper instance.
       "ExampleBind1", -- In order to actually set binds, you must first set the bind name
       {Enum.KeyCode.B, Enum.KeyCode.C, Enum.UserInputType.MouseButton1}, -- Then a table of the selected codes.
       "ExampleBind2", -- You can keep adding them since BindHelper loops over these parameters and sets the bind actions.
        {Enum.KeyCode.D, Enum.KeyCode.E, Enum.UserInputType.MouseButton2}
)

-- However, when you create the BindHelper and the binds, you have to manually and actually bind them to ContextActionService.
-- You can do that with the help of the ":sync" and ":desync" functions.

-- But in order to actually make them work, you need to bind the handler function where the actions are handled.
-- You can do that with the ":setBindFunction" function. You MUST always set the bind function before actually syncing the binds.

KeybindStorage:setBindFunction(path.to.function)
KeybindStorage:syncAll() -- After you have set the bind function, you can either sync all of the current stored binds.
KeybindStorage:sync("ExampleBind1") -- or do it manually

-- Also when i mean by "syncing", i mean the "BindAction" function on the ContextActionService.
-- goes the same for "desyncing" with "UnbindAction"

KeybindStorage:desync("ExampleBind1") -- unbinds the action from the service.

More on the documentation in the module script.

3 Likes