> ## Documentation Index
> Fetch the complete documentation index at: https://rive.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Focus

> Navigate Rive graphics with a keyboard or Apple TV remote in the Apple runtime.

With the Swift Concurrency API, your Rive view joins native focus navigation automatically when the graphic has [focus nodes](https://rive.app/docs/editor/accessibility/focus.md#making-an-element-focusable). The platform moves focus between views. Rive moves focus between elements in the graphic.

## Native focus

Rive discovers focus nodes asynchronously after loading. The graphic can appear before the view can receive focus.

> [!NOTE]
>
> Native focus varies by platform. See Apple's guidance for [iPadOS 15 and later, tvOS, and visionOS](https://developer.apple.com/design/human-interface-guidelines/focus-and-selection/) and [Mac Catalyst](https://developer.apple.com/documentation/uikit/navigating-an-app-s-user-interface-using-a-keyboard). iPhone offers [Full Keyboard Access](https://developer.apple.com/videos/play/wwdc2021/10120/) as a separate accessibility feature.

> [tab] SwiftUI
>
> `RiveUIViewRepresentable` and `AsyncRiveUIViewRepresentable` become focusable automatically when the graphic is ready. Add either view directly to your SwiftUI hierarchy.

> [tab] UIKit
>
> To make `RiveUIView` the preferred target for initial focus, return it from the hosting view controller's `preferredFocusEnvironments`.
>
> ```swift theme={null}
> // In the view controller that hosts riveView.
> override var preferredFocusEnvironments: [any UIFocusEnvironment] {
>     [riveView]
> }
> ```
>
> UIKit considers this preference when it updates focus. If a native control already has focus, loading the graphic leaves focus on that control. See Apple's [UIFocusEnvironment documentation](https://developer.apple.com/documentation/uikit/uifocusenvironment).

> [tab] AppKit
>
> The Rive view joins the window's key-view loop. Use `NSWindow`'s `selectNextKeyView(_:)` or `selectPreviousKeyView(_:)` to move through the loop from code. See [AppKit's key-view APIs](https://developer.apple.com/documentation/appkit/nswindow#Managing-the-Key-View-Loop).

## Requesting keyboard input

Keys go to the element that has focus, and the first element gets focus when the user navigates into the graphic, usually by pressing **Tab**. To send hardware-keyboard input to a graphic without that first **Tab**, such as WASD controls that work as soon as the graphic appears, make the view the keyboard responder.

A request succeeds when the graphic is loaded, playing, in the key window, and has focus nodes. Rive then tries to focus the first element in the [focus order](https://rive.app/docs/editor/accessibility/focus.md#focus-order), unless an element already has focus.

> [!NOTE]
>
> Requesting keyboard input doesn't enable text input or show the software keyboard.

> [tab] SwiftUI
>
> Call `keyboardFocused(_:onResult:)` on `RiveUIViewRepresentable` or `AsyncRiveUIViewRepresentable`, before any SwiftUI modifiers. The view waits until the graphic is ready before it makes the request.
>
> ```swift highlight={5} theme={null}
> @State private var wantsKeyboardInput = true
>
> var body: some View {
>     RiveUIViewRepresentable(rive: rive)
>         .keyboardFocused(wantsKeyboardInput)
> }
> ```
>
> Each change to `true` makes one request, and a change to `false` gives up keyboard input. To request again, such as after you replace the view's content, set the value to `false` and then back to `true` in a later update.
>
> To learn whether an attempt succeeded, pass `onResult`. Rive calls it after each attempt with the action, `.become` or `.resign`, and whether it succeeded.
>
> A change to `true` while the view is paused makes no request. Pausing or removing the view, or a loading failure, cancels a pending request. Rive doesn't call `onResult` in these cases.
>
> Avoid combining `keyboardFocused(_:onResult:)` with SwiftUI's `focused(_:)` on the same view.

> [tab] UIKit
>
> Call `becomeKeyboardResponder()` on `RiveUIView`. It returns whether the request succeeded.
>
> ```swift theme={null}
> let succeeded = riveView.becomeKeyboardResponder()
> ```
>
> The request runs immediately and isn't queued. It returns `false` until the graphic is ready, so a call made as soon as the view appears usually fails. Call it again later, such as when the user first interacts with the graphic.
>
> To give up keyboard input, call `resignKeyboardResponder()`.

> [tab] AppKit
>
> Call `becomeKeyboardResponder()` on `RiveUIView`. It returns whether the request succeeded.
>
> ```swift theme={null}
> let succeeded = riveView.becomeKeyboardResponder()
> ```
>
> The request runs immediately and isn't queued. It returns `false` until the graphic is ready, so a call made as soon as the view appears usually fails. Call it again later, such as when the user first interacts with the graphic.
>
> To give up keyboard input, call `resignKeyboardResponder()`.

On iOS, iPadOS, visionOS, and Mac Catalyst, **Tab** and the arrow keys don't [move between elements](#moving-between-elements) until the view also has native focus. After you replace a view's content, request keyboard input again.

## Moving between elements

* **Tab** and **Shift+Tab** follow the focus order set in the editor.
* **Arrow keys** move focus in the direction you press.
* On **tvOS**, the Siri Remote also moves focus. Rive treats the remote's Select button as **Enter**. See [Apple TV focus interactions](https://developer.apple.com/documentation/uikit/about-focus-interactions-for-apple-tv).

Rive waits for the State Machine to handle each key before deciding whether to pass it back to the platform. [Keyboard listeners](https://rive.app/docs/editor/state-machine/listeners.md#keyboard) get the key before focus navigation does. For example, if Rive leaves **Tab** unhandled at the last element, the platform can move focus to the next native control.

Full Keyboard Access and gamepad navigation outside tvOS fall outside the Apple runtime's focus support. Use [Semantics](https://rive.app/docs/runtimes/apple/semantics.md) for VoiceOver.
