Simulate User Input

userInputService:SimulateUserInput(Enum.UserInputType.MouseButton2)

is there a possible way for the IDEA of this to work? like could i just make a function

SimulateUserInput(Enum.UserInputType.MouseButton2)

Is it possible? and if so how?

There doesn’t appear to be a way on Roblox to do this. What were your plans for using something like this?

4 Likes

It’s not really possible in the way that you are imagining.

What is the scenario of use here?

3 Likes

You can do this easily by using functions!

local function onMousePress()
    -- TODO: Code
end

userInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        onMousePress()
    end
end)

Now, you can call onMousePress whenever you want to ā€œsimulateā€ a mouse press. There’s better ways of doing this, but this is a very simple example.

5 Likes

There is no deliberate way of doing that however whenever you use UserInputService you can just call a function associated with that key and have all logic there and when you want to ā€œsimulateā€ a key press you can just call the function.

3 Likes

Ah yes, mocking things.

I remember trying to do something similar in other languages like C#; normally we would just make an interface and then the implementation. When we needed anything, we would take the interface.

This allows us to then make a ā€˜mock’ implementation and simply control the returns of the implementation without actually doing anything. It should be fairly simple to do this in Luau; you just need to properly define the ā€˜Contract’ (aka Interface) to abide by, and when you don’t need to use it, use the real implementation instead of your class.

I hope this helps you, somewhat.