<!-- https://abap2ui5.github.io/docs/cookbook/browser_interaction/focus -->

# Focus

Read which control currently holds the focus from the backend, or move the focus from the backend to a specific field — both work without any custom control.

## Read the Current Focus

`client->get( )-s_focus` tells you which control currently holds the focus and where the caret sits inside it. Useful when an action depends on the field the user was just editing.

```abap
DATA(focus) = client->get( )-s_focus.

DATA(id)              = focus-id.                " id of the focused control
DATA(selection_start) = focus-selection_start.   " caret start, in chars
DATA(selection_end)   = focus-selection_end.     " caret end, in chars
```

## Set the Focus

Set the input focus from the backend with the `set_focus` frontend event. Pass the target control's `id` as the first argument — the framework moves the cursor to that field after the next roundtrip.

This is useful for guided data entry, barcode scanning, or any flow where the next field to focus depends on backend logic.

## Basic Usage

After processing an event, call `client->follow_up_action( )` with `cs_event-set_focus` and the id of the input to focus next. (The value bindings on the inputs are omitted here to keep the focus logic clear — see [Barcode Scanning](/cookbook/device_capabilities/barcode_scanning) for the same form with bound inputs.)

```abap
CLASS z2ui5_cl_sample_focus DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS z2ui5_cl_sample_focus IMPLEMENTATION.
  METHOD z2ui5_if_app~main.

    IF client->check_on_navigated( ).
      DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
          )->ele( n = `View` ns = `mvc`
              )->a( n = `xmlns`      v = `sap.m`
              )->a( n = `xmlns:mvc`  v = `sap.ui.core.mvc`
              )->a( n = `xmlns:form` v = `sap.ui.layout.form`

              )->ele( `Page`
                  )->ele( n = `SimpleForm` ns = `form`
                      )->ele( n = `content` ns = `form`
                          )->tag( `Label`
                              )->a( n = `text` v = `One`
                          )->tag( `Input`
                              )->a( n = `id`     v = `id1`
                              )->a( n = `submit` v = client->_event( `ONE_ENTER` )
                          )->tag( `Label`
                              )->a( n = `text` v = `Two`
                          )->tag( `Input`
                              )->a( n = `id`     v = `id2`
                              )->a( n = `submit` v = client->_event( `TWO_ENTER` ) ).

      client->view_display( view->stringify( ) ).


    ELSEIF client->check_on_event( `ONE_ENTER` ).
        client->follow_up_action( val   = client->cs_event-set_focus
                        t_arg = VALUE #( ( `id2` ) ) ).

    ELSEIF client->check_on_event( `TWO_ENTER` ).
        client->follow_up_action( val   = client->cs_event-set_focus
                        t_arg = VALUE #( ( `id1` ) ) ).

    ENDIF.

  ENDMETHOD.
ENDCLASS.
```

After the user presses Enter in `id1`, the backend fires `set_focus` for `id2` and the cursor moves to the second input. The same pattern works for any chain of fields.

## Selection Range

To position the caret inside the field or pre-select a range, pass the start and end offsets as additional arguments:

```abap
client->follow_up_action( val   = client->cs_event-set_focus
                t_arg = VALUE #( ( `id1` ) ( `0` ) ( `5` ) ) ).
```

## Barcode Scanning

Most barcode scanner devices emulate a keyboard. Combine `set_focus` with input fields to capture scans into the right field automatically — see [Barcode Scanning](../device_capabilities/barcode_scanning.md) for a full walkthrough.

<!-- samples:start (generated by scripts/link-samples.mjs — do not edit) -->

## Working Samples

Complete apps from the [sample catalog](https://github.com/abap2UI5/samples/blob/main/SAMPLES.md)
that use what this page describes. Each is a single class — pull the repository with
[abapGit](https://abapgit.org) and start it with `?app_start=<class>`.

| Sample | Class |
|---|---|
| Set Focus and Select Text in an Input (A) | [`Z2UI5_CL_SMP_APP_133`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_133.clas.abap) |
| Jump to the Next Input on Enter (A) | [`Z2UI5_CL_SMP_APP_189`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_189.clas.abap) |
| Focus a Table Cell by Column and Row (A) | [`Z2UI5_CL_SMP_APP_421`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_421.clas.abap) |

<!-- samples:end -->
