Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can you make a gui button count as if you preased a keyboard key?

So i made a game. Works preety fine! Now i want it to work on mobile. The scripts may be too complicated to be recreated fully, so is there a way to make a gui button that when pressed counts as if you pressed the keyboard key "e"

2 comments

1

I don’t recommend this, I recommend cleaning up your code to work with both pressing keyboard e and any event you want. I would most likely take out the code from the e event and put it in a function. Then call that function when the button event is fired and the e event is fired.

0

Yeah probably best to make a function and fire it when the trigger for it is pressed/tapped like SethHeinzman said

1 answer

You can’t. But you can mobile buttons with ContextActionService. It is very similar to UserInputService, except you can disable the function, create a mobile button, and won’t run if you’re typing in the chat. Here’s the tutorial and documentation about it.

You can create one by ContextActionService:BindAction(). It has four parameters: actionName, functionToBind, createTouchButton, and inputTypes. actionName, as it name said, is the name of the action. It must be a string, other it would error. functionToBind is the function that will run when the mobile button/keyboard button is pressed. createTouchButton is the boolean whether the action should make a touch button (for mobile) or not (if the game is exclusive on pc/console). inputTypes are the inputTypes (more specifically Enum.KeyCode) that will activate the action when they are pressed.

For this example, we’ll make a button that makes a part change colors from blue to red, and vice versa.

ContextActionService:BindAction("changePartColor", function()
   
end, true, Enum.KeyCode.B, Enum.KeyCode.R)

The functionToBind receives the arguments actionName, inputState, and inputObject. actionName is the name as the name or the action, inputState is the input state of the inputObject pressed, and inputObject is the input that caused the action to activate.

ContextActionService:BindAction("changePartColor", function(actionName, inputState, inputObject)
   if inputObject.UserInputType == Enum.UserInputType.Keyboard then
      if inputObject.KeyCode == Enum.KeyCode.B then
         Part.BrickColor = BrickColor.new("Bright blue")
      elseif inputObject.KeyCode == Enum.KeyCode.R then
         Part.BrickColor = BrickColor.new("Bright red")
      end
   elseif inputObject.UserInputType == Enum.UserInputType.Touch then
      if Part.BrickColor == BrickColor.new("Bright blue") then
         Part.BrickColor = BrickColor.new("Bright red")
      else
         Part.BrickColor = BrickColor.new("Bright blue")
      end
   end
end, true, Enum.KeyCode.B, Enum.KeyCode.R)

You can disable the action by ContextActionService:UnbindAction()

ContextActionService:UnbindAction("changePartColor")

And if you prefer UserInputService, make yourself a GuiButton, and when the button is pressed, fire it to the input script using BindableEvents.

--Button Script
Button.MouseButton1Click:Connect(function()
   BindableEvent:Fire()
end)

--Input Script
UserInputService.InputBegan:Connect(function(inputObject)
   if inputObject.UserInputType == Enum.UserInputType.Keyboard then
      if inputObject.KeyCode == Enum.KeyCode.B then
         Part.BrickColor = BrickColor.new("Bright blue")
      elseif inputObject.KeyCode == Enum.KeyCode.R then
         Part.BrickColor = BrickColor.new("Bright red")
      end
   end
end)

BindableEvent.Event:Connect(function()
   if Part.BrickColor == BrickColor.new("Bright blue") then
      Part.BrickColor = BrickColor.new("Bright red")
   else
      Part.BrickColor = BrickColor.new("Bright blue")
   end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.