UserInput Module — Overview
Module: UserInput
This module provides an advanced way to handle user input in Roblox, similar to an InputContext, but directly built on top of UserInputService.
It supports both keyboard and gamepad inputs, allowing dynamic switching between them at runtime.
It is inspired by Sleitnick’s Input Framework and is designed for developers who want fine-grained control over which keys are tracked and how input events are handled.
Key Features
- Multiple Input Types
- Supports
"Keyboard"or"Gamepad". - Can switch input type at runtime with
ChangeInputType()(keys are cleared during the switch).
- Key Management
KeysRegisteredstores all active key bindings.- Methods for:
.ChangeKey()— replace all keys..AddKey()— add keys without duplicates..RemoveKey()— remove keys selectively.
- Signal-Based Input Events
.Pressed(callback)— triggered when a registered key is pressed..Released(callback)— triggered when a registered key is released.- Signals ensure clean, reusable connections.
- Lifecycle Control
.DisconnectPressed()and.DisconnectReleased()— stop listening to events..Destroy()— cleans up signals, registered keys, and the input structure.
API Summary
Public
KeysRegistered: {Enum.KeyCode}— array of registered keys.ChangeInputType(type: "Gamepad" | "Keyboard")— switches between input devices.ChangeKey(keys)— replaces all registered keys.AddKey(keys)— adds keys to the list.RemoveKey(keys)— removes keys from the list.Pressed(callback)— connects a function to key-press events.Released(callback)— connects a function to key-release events.DisconnectPressed()— disconnects.Pressedsignals.DisconnectReleased()— disconnects.Releasedsignals.Destroy()— fully destroys the input component.
Private
__setUpInputStructKeyPressed()— internal setup for key press detection.__setUpInputStructKeyReleased()— internal setup for key release detection.
Example Usage
lua
local Input = UserInput.new({
Keys = {Enum.KeyCode.A, Enum.KeyCode.B},
InputType = "Keyboard"
})
Input:Pressed(function(key)
print("Pressed:", key)
end)
Input:Released(function(key)
print("Released:", key)
end)
-- Switch to gamepad at runtime
Input:ChangeInputType("Gamepad")
Design Notes
- Abstraction Layer: Separates keyboard/gamepad logic through
inputStruct, allowing easy future expansion. - Signal-Based Architecture: Clean event handling, easy subscription/unsubscription.
- Runtime Flexibility: Input type and keys can be updated without restarting the game session.
Full documentation
- See the full documentation here: UserInput