How to detect a button click or a key press on a same function?

So, I’m trying to make a button that has both PC and mobile compatibility that opens a panel.

However, I need it to be specifically as this:

  • For the PC, it can be operated by a key press
  • For mobile, it can be operated by pressing the button

I know how to make both individually, but how could I make both of these things work with a same function without duplicating the code countless amount of times? I’ve seen some developers that pulled this off.

4 Likes

just make your function a local variable.

local function OnKeyPressOrClick()
--code here
end
1 Like

Alright, that is also one variant of how to make it work and I’ll try it out later.

However, is there a way to detect separate actions?

Make your function a global variable, and then call it at the end of the script using UserInputService or ContextActionservice. this is the only way as I’m aware.

1 Like

How would I incorporate UserInputService with clicking a button on a touch screen tho? Like, how do I connect a specific button to it?

Look into the UIS article on the DevHub. There’s also an article regarding Cross-Platform development

2 Likes

I checked it out and here’s what I came up with so far, but how do I also incorporate the mobile action?

game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
	if not Processed then
		if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
			print("R has been pressed")
		elseif input.UserInputType == Enum.UserInputType.Touch then
			--something was touched on a touchscreen, but how do I get the button
		end
	end
end)

Still haven’t figured it out, any help is appreciated.

What do you mean by “how do I get the button”?
If you want it to be activated by a button then just do button.MouseButton1Click

I want to use UserInputService cause it then allows me to do this:

I dont think you can get the specific button that was pressed from any script.
Your best bet would be this:

game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
	if not Processed then
		if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
			print("R has been pressed")
            someFunction()
		end
	end
end)

script.Parent.Button.MouseButton1Click:Connect(function()
   if game:GetService("UserInputService").TouchEnabled == true then
      someFunction()
   end
end)
7 Likes

A lot of major developers rely on UserInputService for mobile support, so I feel like it’s the best approach using UserInputService. I think it exists.

What about this?

game:GetService("UserInputService").InputBegan:connect(function(input, Processed)
	if not Processed then
		if input.KeyCode == Enum.KeyCode.R then --a PC key was pressed
			print("R has been pressed")
            someFunction()
		end
	end
end)

Frame.InputBegan:connect(function(inputObj)
	if inputObj.UserInputType == Enum.UserInputType.Touch then --a mobile button was touched
		print("Gui has been touched!")
        someFunction()
	end
end)

wait nvm you’re right, it doesn’t exist for UserInputService

I’ll go with the suggestion from you.

1 Like