Input everywhere, part 1: keyboard, gamepad, touch and accessibility

Make a Roblox game work on keyboard, gamepad and touch. Bind Beacon Yard's wrench swing everywhere, meet the Input Action System, add input remapping, gamepad UI selection, and accessibility for text size, colour and reduced motion.

Concept lesson, about 75 minutes, 11 steps

By the end you can

  • Bind one action to keyboard, gamepad and touch, and pick an aim path from the player's input device
  • Say what the Input Action System's contexts, actions and bindings are, and let players remap a binding safely
  • Make menus work with gamepad selection
  • Respect players' text size, transparency and reduced-motion settings, and never signal with colour alone

Before this: The wrench: tools, raycasts, tags, Animation: the Animation Editor, Moon Animator, Animator

Step 1 of 11

One swing, three devices

Open Studio's device emulator and pick a phone. A round Swing button appears on the right of the screen. Tap it and the wrench swings, exactly as F does on a keyboard and the right trigger does on a gamepad. The majority of Roblox sessions are played on phones and tablets1, so this button is not an extra.

The emulated phone shows the WrenchSwing touch button; a tap fires the same remote as F.

Transcript
  1. 0:10 The phone emulator shows the WrenchSwing touch button.
  2. 0:13 The title is Swing.
  3. 0:15 The tap fires the same Swing remote as the F key.
Choose one

A phone player taps Swing, a PC player presses F. What reaches the server?

A phone player taps Swing, a PC player presses F. What reaches the server?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 11

One action, many inputs

CombatClient binds a named action, WrenchSwing, with ContextActionService:BindAction. The third argument, true, asks Roblox to draw a touch button; the rest are the keys that trigger it2.

	ContextActionService:BindAction(
		"WrenchSwing",
		function(_name: string, state: Enum.UserInputState, _input: InputObject)
			if state ~= Enum.UserInputState.Begin then
				return Enum.ContextActionResult.Pass
			end
			fireSwing()
			return Enum.ContextActionResult.Sink
		end,
		true,
		Enum.KeyCode.F,
		Enum.KeyCode.ButtonR2
	)
	ContextActionService:SetTitle("WrenchSwing", "Swing")

The handler only acts on UserInputState.Begin, so one press is one swing, and returns Sink so the press does not also fall through to other bindings. The Tool.Activated event is a third door into the same fireSwing, for a click or tap while the tool is equipped.

Roblox Studio
Roblox Studio's device emulator showing Beacon Yard on an emulated phone, with the HUD scaled down, a movement thumbstick at the bottom left and the Swing touch button at the bottom right.

Select a numbered pin, or a row below, to read what it marks.

The device emulator. Select the pins to find the Swing button and the scaled HUD.

Choose one

The WrenchSwing handler returns Enum.ContextActionResult.Pass for every state except Begin. Why?

The WrenchSwing handler returns Enum.ContextActionResult.Pass for every state except Begin. Why?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 11

Aim depends on the device

A mouse can point anywhere on screen; a thumb on glass or a thumbstick cannot. aim picks a path with UserInputService.MouseEnabled. With a mouse, it casts a ray from the cursor. Without one, it uses the head and the camera's look direction.

	if not UserInputService.MouseEnabled then
		local look = unitOrNil(camera.CFrame.LookVector)
		if look == nil then
			return nil, nil
		end
		return headPos, look

It does not test TouchEnabled. Touch-screen laptops report TouchEnabled and still have a mouse, so "has touch" does not mean "is a phone"3. For UI that should change with the device the player is using right now, read UserInputService.PreferredInput, which follows the player's most recent input.

Choose one

A player on a touch-screen laptop swings with the mouse. Which aim path does Beacon Yard use?

A player on a touch-screen laptop swings with the mouse. Which aim path does Beacon Yard use?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 11

The newer way: the Input Action System

The ContextActionService reference now says BindAction is not recommended for new work and is planned for deprecation2. Its replacement is the Input Action System: instead of calling a function, you create instances4.

  • An InputContext groups actions, such as PlayContext for gameplay and MenuContext for an open menu. Enabled switches the whole group. Priority decides which context hears a key first, and Sink stops that key from reaching contexts below it.
  • An InputAction is one thing the player does, such as WrenchSwing. With Type set to Bool, it fires Pressed once per press and Released once per release.
  • Each InputBinding under an action is one way to do it: a KeyCode such as F or ButtonR2, or a UIButton that points at a GuiButton for touch. The system draws no touch button for you. You make the button and link it.

Here is the swing as instances, built from code in a client script:

luau
local play = Instance.new("InputContext")
play.Name = "PlayContext"
play.Parent = inputsFolder -- ReplicatedStorage.Inputs, as the guide recommends
 
local swing = Instance.new("InputAction")
swing.Name = "WrenchSwing"
swing.Type = Enum.InputActionType.Bool
swing.Parent = play
 
for _, key in { Enum.KeyCode.F, Enum.KeyCode.ButtonR2 } do
	local binding = Instance.new("InputBinding")
	binding.KeyCode = key
	binding.Parent = swing
end
local touch = Instance.new("InputBinding")
touch.UIButton = swingButton -- a TextButton you placed in a ScreenGui
touch.Parent = swing
 
swing.Pressed:Connect(fireSwing)

Open Window > Input > Actions to edit contexts, actions and bindings in the Input Action Manager. InputActionLabel is a Studio beta. In a published game, use InputAction.PreferredBinding to get the current binding and render its key or button hint.

Predict the output

The m06 WrenchSwing handler checks the input state and returns Pass or Sink. Connected to swing.Pressed instead, what does it need?

swing.Pressed:Connect(fireSwing)
The m06 WrenchSwing handler checks the input state and returns Pass or Sink. Connected to swing.Pressed instead, what does it need?

Press 1 to 4 to choose, Enter to check.

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Input Action SystemInputContext, InputAction and InputBinding instances, set up in Studio or from code.New games and anything with menus that should switch controls, rebinding, or on-screen prompts.Newer, so fewer tutorials and community examples, and you build your own touch buttons.
ContextActionServiceBindAction with a callback, keys and an optional touch button.Existing code such as Beacon Yard's Controls.luau, which still uses it at m14-ship.Not recommended for new work and planned for deprecation.
UserInputService eventsInputBegan and InputEnded for every raw input.Custom cameras, drag gestures and anything an action map cannot express.You handle every device and every conflict yourself.
Match the pairs

Match each job to the tool that fits it best.

Link to this step · This confused me

Step 5 of 11

Let players remap

Many players need a different key: left-handed players, players with one hand, players whose keyboard has no easy F. With the Input Action System, remapping is changing a property. InputBinding.KeyCode is writable at runtime, so a settings menu can listen for the next key and assign it. Save the choice in the player's profile and apply it on join.

Two rules keep remapping safe. Reserved inputs such as Esc and F9 (an example list) cannot be bound. And a new key must not silently steal one that another action uses.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Choose one

A player rebinds WrenchSwing to Q and your menu allows it without a check. What breaks in Beacon Yard?

A player rebinds WrenchSwing to Q and your menu allows it without a check. What breaks in Beacon Yard?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 6 of 11

Menus on a gamepad

A gamepad has no cursor. To use a menu, the player moves a selection from button to button with the stick or D-pad and presses a button to activate it. GuiService.SelectedObject is the element that currently has the selection5. GuiButtons are selectable by default; NextSelectionUp, Down, Left and Right override where the stick goes.

The m06 shop does not set a selection when it opens. A gamepad player has to press the Select button to enter UI navigation before they can buy anything. The m07 fix is three lines: when the shop opens, set GuiService.SelectedObject to the first button; when it closes, set it to nil; and let ButtonB close it.

Find the broken line

A gamepad player opens the shop and can buy, but after closing it their thumbstick still moves an invisible selection instead of the character. Which line is missing its partner?

A gamepad player opens the shop and can buy, but after closing it their thumbstick still moves an invisible selection instead of the character. Which line is missing its partner?

Press 1 to 7 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 11

Settings players already chose

Players set preferences in Roblox's own settings menu, and your UI can read them6:

  • Text size. GuiService.PreferredTextSize grows text rendered at a fixed TextSize. Text with TextScaled on does not grow. Beacon Yard's HUD uses fixed sizes (22, 18, 16), so it follows the setting.
  • Background transparency. GuiService.PreferredTransparency is 1 by default and 0 for players who want solid panels. Multiply your BackgroundTransparency by it.
  • Reduce motion. GuiService.ReducedMotionEnabled is true for players who want less movement. CameraClient tweens the field of view over 0.35 seconds when you press Q; with reduced motion on, it should snap or fade instead.
  • Colour. Over 5% of people have some colour blindness. Never let colour alone carry meaning: a stunned drone turns white and gets an outline, and the shop's buttons are coloured and labelled.
Reference Player preference properties

Listen with GetPropertyChangedSignal on each, because players can change them during a session.

Enum.PreferredTextSize
Medium by default; Large, Larger or Largest when the player wants bigger text.
number, 0 to 1
1 by default; 0 when the player wants opaque backgrounds. Multiply BackgroundTransparency by it.
boolean
True when the player wants motion reduced or removed.
GuiObject?
The element gamepad navigation has selected; set it when a menu opens, clear it when it closes.
Enum.PreferredInput
Touch, KeyboardAndMouse or Gamepad, following the player's most recent input.
Choose all that apply

Which of these respect the player's settings and needs?

Which of these respect the player's settings and needs?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 11

Where the front desk picture breaks

Both sidesRemember: A RemoteEvent is a note to the front-desk clerk.

Three devices, one note to the front desk. The clerk does not care whether you wrote it with a pen, a keyboard or a thumb. Answer first, then open the breakdown.

Where does it break?“A note to the front-desk clerk” is a good picture of a remoteevent. Where does it stop being true?

It breaks here. The engine stamps the sender for you, but every other word on the note can be a lie.

The true model. A RemoteEvent carries a one-way message between a client and the server. The server always learns which player sent it; every other argument is untrusted input that the server must check.

Choose one

Someone suggests letting touch players send a bigger WRENCH_RANGE in the Swing remote, because aiming on glass is harder. Why is that a bad idea?

Someone suggests letting touch players send a bigger WRENCH_RANGE in the Swing remote, because aiming on glass is harder. Why is that a bad idea?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 9 of 11

Build it: input and accessibility for m07

  • src/client/Controls.luau
  • src/client/Accessibility.luau
  • src/client/CombatClient.luau
  • src/client/CameraClient.luau
  • src/client/ShopGui.luau
  1. Press Play, open the device emulator from the Test tab and choose a phone.

    You should see The emulator shows the phone layout with a Swing button; tapping it swings and the drone takes damage during a drill.

  2. Check out m07-feel and inspect src/client/Controls.luau and git diff m06-drone-wrench m07-feel -- src/client/CombatClient.luau. Notice that m07-feel puts every binding in Controls.luau. That one file is what keeps the move to the Input Action System small, as part 2 shows.

    You should see Controls.luau holds one BINDINGS table for WrenchSwing, CameraFieldOfView, OpenSettings and CloseShop, and wraps ContextActionService:BindAction so CombatClient, CameraClient and ShopGui all bind through Controls.bind.

  3. Open src/client/Accessibility.luau and src/client/CameraClient.luau. Accessibility.start() seeds the ReducedMotion player attribute from GuiService.ReducedMotionEnabled, and CameraClient checks Accessibility.reducedMotion() to snap instead of tweening. Toggle Reduce Motion in the Roblox settings menu or the yard's Settings panel and press Q.

    You should see Pressing Q (or Y on a gamepad, or Zoom on touch) with ReducedMotion on snaps camera.FieldOfView directly instead of tweening.

  4. Open src/client/ShopGui.luau and find where it sets GuiService.SelectedObject = speedButton, binds CloseShop (ButtonB) while open, and clears SelectedObject when closing.

    You should see Opening the shop with a gamepad highlights the Speed button at once; ButtonB closes it via CloseShop and clears GuiService.SelectedObject so the thumbstick moves the character again.

  5. Open the yard's Settings panel (P on keyboard, DPadUp on gamepad, or the Settings touch button) to toggle Large text and check how Accessibility.text rescales the HUD and shop.

    You should see With LargeText on (or Roblox's Text Size raised), the registered HUD and shop labels scale up and still fit their panels.

Link to this step · This confused me

Step 10 of 11

Challenges

Level 1, Apprentice

Challenge

Rename the touch button and find it in the emulator.

Start from
The game at milestone m06 (download)

Done means

  • In the device emulator, the touch button reads a new title
  • The title is Swing again afterwards

Meet the criteria above, then record your own result. No test grades this check.

Level 2, Builder

Challenge

Make the field-of-view zoom respect Reduce Motion.

Start from
The game at milestone m06 (download)

Done means

  • With Reduce Motion on, Q changes the field of view with no tween
  • With it off, the 0.35-second tween is unchanged
  • Changing the setting during Play takes effect on the next press

Meet the criteria above, then record your own result. No test grades this check.

Choose one

A tablet player connects and starts using a Bluetooth gamepad. Which property tells your UI to show gamepad button prompts?

A tablet player connects and starts using a Bluetooth gamepad. Which property tells your UI to show gamepad button prompts?

Press 1 to 4 to choose, Enter to check.

Level 3, Professional

Challenge

Add a YardHint action with ContextActionService without disturbing the wrench. (Use ContextActionService: the test reads it.)

Start from
The game at milestone m06 (download)
Grade it
To grade work from an earlier milestone, apply your challenge changes to a checkout based on m14-ship, which includes the Course Runner. In Studio, press Play, then run require(game.ReplicatedStorage.GameTests.RunChallenge)("input-mobile", "input-yard-hint") in the client command bar. Copy the pass code from Output.

Done means

  • A second action named YardHint is bound on the client with ContextActionService:BindAction
  • YardHint creates a touch button (createTouchButton = true) and binds Enum.KeyCode.G and Enum.KeyCode.ButtonL2
  • The WrenchSwing binding via ContextActionService is unchanged: touch button, F and ButtonR2
  • challenges/input-mobile.luau, run on the client, prints TEST PASS yard_hint

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 11 of 11

Recap

Recap

  1. One named action takes keyboard, gamepad and touch, and every device sends the same Swing remote; aim branches on MouseEnabled, and PreferredInput follows the current device.
  2. BindAction is legacy; the Input Action System's contexts, actions and bindings make remapping a property change, with reserved keys and conflicts checked.
  3. Menus set and clear GuiService.SelectedObject for gamepads, and UI respects text size, transparency, reduced motion and never relies on colour alone.
  4. Controls.luau holds every binding in one table, which is what keeps the move to the Input Action System in part 2 small.

A RemoteEvent is a note to the front-desk clerk, until the engine stamps the sender for you, but every other word on the note can be a lie.

Next Input everywhere, part 2: move the yard to the Input Action System

Link to this step · This confused me