UserInputService
’s events are universal, meaning they fire for every input, even if the callback doesn’t do anything with it.
That means to make the console print something every time the player presses E, you would have to filter out the Input.KeyCode
and WasProcessed
arguments every callback.
local function OnInputBegan(Input: InputObject, WasProcessed: boolean)
if Input.KeyCode == Enum.KeyCode.E and not WasProcessed then
print("Pressed E")
end
end
UserInputService.InputBegan:Connect(OnInputBegan)
The Proposal
Just like Instance/GetPropertyChangedSignal
, add methods to UserInputService
that return dedicated signals for the specified UserInputTypes and KeyCodes.
For example, a GetKeyCodeBeganSignal
would have the signature (keyCode: Enum.KeyCode, processed: boolean?) -> RBXScriptConnection
.
local function OnPressed()
print("Pressed E")
end
UserInputService:GetKeyCodeBeganSignal(Enum.KeyCode.E, false):Connect(OnPressed)
If processed
is true
/false
, it will only fire for that gameProcessedEvent
. Otherwise, it allows all inputs.
This would also be extended for the Ended
and Changed
variants, as well.