Converting or Translating a (Mobile) Button Touch into PC Keys - UserInputService

Hi, as a novice script developer, I want to ask how to turn button clicks into a computer key? After some trial and error, I’ve discovered that the easiest approach to create mobile buttons (Mobile Support) is to translate or convert (Gui) Button Clicks into Keys—more precisely, PC Keys like E, R, MouseButton1, etc. Using or Employing UserInputService.

I’ve tried searching on DevForum and even using ChatGPT, but I’m still not sure how. If someone could show me or maybe teach me, that would be really appreciated.

Here is some code that matches my skill level

local UserInputService = game:GetService("UserInputService")

local button = script.Parent

-- Function to simulate mouse button press
local function simulateMouseClick()
    local inputBegan = {}
    inputBegan.UserInputType = Enum.UserInputType.MouseButton1

    UserInputService.InputBegan:Fire(inputBegan)
end

button.MouseButton1Click:Connect(function()
    -- Simulate pressing the left mouse button
    simulateMouseClick()
end)


Most times it bugs out. and gives me an error.

It is not possible to simulate keyboard inputs in Roblox. Take a look at ContextActionService instead and binding functions to get something similar.

1 Like

UserInputService.InputBegan doesn’t work like that. It’s not equivalent to a RemoteEvent. It is an event used to detect whenever the client makes an input.

What I recommend instead is make a function and connect it to both events.

local function leftMouseClick()
	print("User left clicked!")
end

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then -- the input is left click
		leftMouseClick()
	end
end)

button.MouseButton1Click:Connect(leftMouseClick)
1 Like

This is the most probable solution becuase you can bind functions with more than one key code or user input type

1 Like