KeybindHandler - Simpler Keybind Mapping

I’ve created this simple module to help with mapping Keybinds. This would be useful for combat games and any other games that need remappable Keybinds. This is my first module I’ve posted on here so I’d really appreciate feedback on how I could improve it.

Here’s the pastebin

:arrows_counterclockwise: ChangeKeyBind

ChangeKeyBind(name, newKeyCode)

Rebinds an existing keybind to a new key.


:x: UnbindKeybind

UnbindKeybind(name)

Removes a keybind by name.


:pause_button: PauseKeybind

PauseKeybind(name)

Temporarily disables the keybind without removing it.


:arrow_forward: UnPauseKeybind

UnPauseKeybind(name)

Re-enables a previously paused keybind.


:male_detective: CheckKeybind

CheckKeybind(name)

Returns the current Enum.KeyCode for the keybind (or nil if not found).


:dart: SetKeyDownCallback

SetKeyDownCallback(name, callback)

Sets the function to run when the key is pressed.


:dart: SetKeyUpCallback

SetKeyUpCallback(name, callback)

Sets the function to run when the key is released.


:test_tube: FireKeybindDown

FireKeybindDown(name)

Manually triggers the key down callback for testing/debugging.


:test_tube: FireKeybindUp

FireKeybindUp(name)

Manually triggers the key up callback.


:headphones: ActivateListener

ActivateListener()

Starts listening for key input events using UserInputService.InputBegan and InputEnded.

You must call this to begin detecting inputs!


:stop_sign: DeactivateListener

DeactivateListener()

Disconnects the listeners and cleans up.


:clipboard: GetAllKeybinds

GetAllKeybinds()

Returns the internal keybind table.


:gear: SETTINGS

Inside the module:

lua

CopyEdit

SETTINGS = {
    WARN = false -- Set to true to enable warnings for missing keybinds or paused keys
}

When WARN = true, invalid operations like rebinding nonexistent keys or firing paused keybinds will trigger a warn().
__

Example Pattern

KeybindHandler.BindKeyBind("Inventory", Enum.KeyCode.I)

KeybindHandler.SetKeyDownCallback("Inventory", function()
    print("Opening inventory...")
end)

KeybindHandler.ActivateListener()
4 Likes