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

1One 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.

Figure 1.1.

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.

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

  1. The same Swing remote with an origin and a direction, from both
  2. A touch event from the phone and a key event from the PC
  3. Different remotes, one per device
  4. Only the PC player's swing; touch is client-only
Answer

The same Swing remote with an origin and a direction, from both. Every device ends in fireSwing, which fires Swing with two vectors. The server never needs to know which device was used.

Open this step in lesson mode · Link to this step · This confused me

2One 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")
Figure 2.1

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.

Figure 2.2.

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

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

  1. Releases and other states are not swings, so other bindings may use them
  2. Pass makes the swing stronger
  3. Pass is required or the button disappears
  4. Pass sends the input to the server
Answer

Releases and other states are not swings, so other bindings may use them. Only the press is a swing. Passing the rest keeps the binding from swallowing input it does not use.

Open this step in lesson mode · Link to this step · This confused me

3Aim 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
Figure 3.1

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.

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

  1. The mouse ray, because MouseEnabled is true
  2. The camera look vector, because TouchEnabled is true
  3. Neither; the swing is rejected
  4. Whichever input arrived first in the session
Answer

The mouse ray, because MouseEnabled is true. aim branches on MouseEnabled. A laptop with a mouse aims with the cursor even though it also has a touch screen.

Open this step in lesson mode · Link to this step · This confused me

4The 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.

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)
  1. Nothing extra: Pressed fires once per press with no state, so fireSwing is the whole handler
  2. The same Begin check, because Pressed also fires on release
  3. A return value of Sink, or the key reaches other actions
  4. A BindAction call as well, to keep the touch button
Answer

Nothing extra: Pressed fires once per press with no state, so fireSwing is the whole handler. Pressed and Released split the states for you, and Sink moved to the context. That is why the migration in part 2 deletes code from the handlers.

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 each job to the tool that fits it best.

  1. A new Dash action on keyboard, gamepad and touch, rebindable
  2. Keep m06's WrenchSwing working while you migrate
  3. A pinch-to-zoom gesture on the map screen
Answer

A new Dash action on keyboard, gamepad and touch, rebindable: Input Action System; Keep m06's WrenchSwing working while you migrate: ContextActionService; A pinch-to-zoom gesture on the map screen: UserInputService events. New actions belong in the Input Action System. Existing BindAction code keeps working until it is migrated. Gestures that are not simple presses use raw input events.

Open this step in lesson mode · Link to this step · This confused me

5Let 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
local bindings = { WrenchSwing = "F", CameraFieldOfView = "Q", Interact = "E" }
local RESERVED = { Escape = true, F9 = true, F11 = true, F12 = true }
 
local function remap(action, key)
	if RESERVED[key] then
		return false, key .. " is reserved"
	end
	for other, used in bindings do
		if other ~= action and used == key then
			return false, key .. " is already " .. other
		end
	end
	bindings[action] = key
	return true, action .. " is now " .. key
end
 
print(remap("WrenchSwing", "Q"))
print(remap("WrenchSwing", "F9"))
print(remap("WrenchSwing", "G"))
Figure 5.1. Expected output: false Q is already CameraFieldOfView false F9 is reserved true WrenchSwing is now G

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

  1. Q is already bound to the camera, so the actions compete for the same key
  2. Nothing; actions can share keys
  3. The server rejects swings from Q
  4. Touch players lose the Swing button
Answer

Q is already bound to the camera, so the actions compete for the same key. CameraFieldOfView already uses Q. A remap screen must detect the clash and ask, or the player loses the zoom or gets two actions per press.

Open this step in lesson mode · Link to this step · This confused me

6Menus 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.

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?

local function openShop()
	shop.Visible = true
	GuiService.SelectedObject = speedButton
end
local function closeShop()
	shop.Visible = false
end
Answer

Line 6: shop.Visible = false. Fixed: shop.Visible = false GuiService.SelectedObject = nil. Opening sets the selection; closing must clear it, or navigation stays on a hidden button. Set SelectedObject to nil when the menu closes.

Open this step in lesson mode · Link to this step · This confused me

7Settings 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.

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

  1. TweenInfo.new(if GuiService.ReducedMotionEnabled then 0 else 0.35) for the FOV change
  2. Labels with a fixed TextSize, so PreferredTextSize can grow them
  3. Showing the drone's stun only by turning it white
  4. TextScaled on every label, so text always fills its box
Answer

TweenInfo.new(if GuiService.ReducedMotionEnabled then 0 else 0.35) for the FOV change; Labels with a fixed TextSize, so PreferredTextSize can grow them. A zero-time tween snaps for reduced-motion players, and fixed text sizes follow the text-size setting. TextScaled ignores PreferredTextSize, and colour alone fails colour-blind players; Beacon Yard adds an outline.

Open this step in lesson mode · Link to this step · This confused me

8Where 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.

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?

  1. The server cannot verify which device sent the note, so every cheater would claim to be on touch
  2. Touch devices cannot send numbers
  3. The range is already infinite on touch
  4. Remotes cannot carry more than two arguments
Answer

The server cannot verify which device sent the note, so every cheater would claim to be on touch. The server knows who sent a remote, not what they used. Make touch easier on the client (aim assist, a bigger button) and keep the server's checks the same for everyone.

Open this step in lesson mode · Link to this step · This confused me

9Build 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.

Open this step in lesson mode · Link to this step · This confused me

10Challenges

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

Hints

  1. SetTitle is called right after BindAction.
  2. Do not add a second BindAction for the wrench.

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

Hints

  1. Read GuiService.ReducedMotionEnabled at the moment of the press, not once at start.
  2. A TweenInfo with time 0 snaps.

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

  1. UserInputService.PreferredInput
  2. UserInputService.TouchEnabled
  3. UserInputService.MouseEnabled
  4. GuiService.SelectedObject
Answer

UserInputService.PreferredInput. PreferredInput reports Gamepad once a gamepad is connected and used, even on a touch device. TouchEnabled would still be true.

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

Hints

  1. Bind YardHint in a new client module, not inside CombatClient, using ContextActionService:BindAction (the test reads ContextActionService:GetAllBoundActionInfo(), so an Input Action System binding will not be seen).
  2. Pass true for createTouchButton, plus Enum.KeyCode.G and Enum.KeyCode.ButtonL2.
  3. Run the check from the client command bar during Play.

Open this step in lesson mode · Link to this step · This confused me

11Recap

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

Open this step in lesson mode · Link to this step · This confused me