Setting the Cursor from within the Program 

You can set the cursor on the current list dynamically from within your program. You can do this to support the user with entering values into input fields or selecting fields or lines. If input fields occur on a list, the system by default places the cursor into the first input field.

To set the cursor, use the SET CURSOR statement. This statement sets the cursor in the most recently-created list. While the basic list is being created, this is always the basic list itself. For a detail list, it is the previous list.

With SET CURSOR, you can set the cursor to an absolute position, to a field, or to a line.

Setting the Cursor Explicitly

To set the cursor to a certain position in the output window, use:

SET CURSOR <col> <lin>.

This statement sets the cursor to column <col> of line <lin> of the output window.

The system sets the cursor only to positions that are visible in the display. For positions outside the displayed area, it ignores the statement. To set the cursor to a part of the list currently not displayed, you must scroll the list first.

You can set the cursor only to lines that contain list output. These include lines skipped with the SKIP statement, but no underlines. If <lin> is below the bottom list line, the system sets the cursor to the bottom line.

REPORT demo_list_set_cursor_1 NO STANDARD PAGE HEADING LINE-SIZE 80.

SET PF-STATUS 'SCROLLING'.

NEW-LINE NO-SCROLLING.
WRITE 'Input Fields:'.
NEW-LINE NO-SCROLLING.
WRITE '-------------'.

NEW-LINE.
DO 5 TIMES.
  WRITE:  ' Input', (1) sy-index, '   ' INPUT ON NO-GAP.
ENDDO.

FORMAT INPUT OFF.
SET CURSOR 11 3.

AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'LEFT'.
      IF sy-staco > 1.
        SCROLL LIST LEFT BY 12 PLACES.
      ENDIF.
    WHEN 'RGHT'.
      IF sy-staco < 40.
        SCROLL LIST RIGHT BY 12 PLACES.
      ENDIF.
  ENDCASE.
  SET CURSOR 11 3.

This program creates a basic list that contains five input fields. The cursor is set to the first input field. The user can use the pushbuttons LEFT and RIGHT to scroll the list horizontally. After each scroll movement, the cursor is set to an input field again.

Setting the Cursor to a Field

To set the cursor to a certain field on a line of the displayed list, use:

SET CURSOR FIELD <f> LINE <lin> [OFFSET <off>].

This statement sets the cursor on line <lin> onto the field whose name is stored in <f>. If a field appears more than once on a line, the system sets the cursor to the first field. If the field does not appear on the line or if it is outside the displayed area, the system ignores the statement. You can use the SCROLL statement to scroll the line into the visible area of the screen.

Use the OFFSET option to set the cursor to position <off> of the field stored in <f>. <off> = 0 indicates the first position.

When you set the cursor, you must take into account the header lines of the list.

REPORT demo_list_set_cursor_2 LINE-SIZE 50.

DATA: input1(5) TYPE c VALUE '*****',
      input2(5) TYPE c VALUE '*****',
      input3(5) TYPE c VALUE '*****'.

SET PF-STATUS 'INPUT'.

WRITE   'Input Fields:'.
WRITE / '-------------'.
SKIP.

WRITE: 'Input 1', input1 INPUT ON,
     / 'Input 2', input2 INPUT ON,
     / 'Input 3', input3 INPUT ON.

AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'INP1'.
      SET CURSOR FIELD 'INPUT1' LINE 6 OFFSET 4.
    WHEN 'INP2'.
      SET CURSOR FIELD 'INPUT2' LINE 7 OFFSET 4.
    WHEN 'INP3'.
      SET CURSOR FIELD 'INPUT3' LINE 8 OFFSET 4.
  ENDCASE.

This program creates a basic list containing three input fields. In the status INPUT, the function codes INP1, INP2, and INP3 are assigned to the function keys F5 , F6, and F7 and to the application toolbar. When you choose one of these functions, the system triggers the AT USER-COMMAND event. It places the cursor on the corresponding input field.

Setting the Cursor to a Line

To set the cursor to a certain line of the list in the output window, use:

SET CURSOR LINE <lin> [OFFSET <off>].

This statement sets the cursor to line <lin>. The system ignores the statement, if the line does not appear in the list or in the visible area of the window. You can use the SCROLL statement to scroll the line into the visible area of the screen.

Use the OFFSET option to set the cursor to column <off> of line <lin>. <off> = 0 indicates the first column. The system ignores the statement, if the position is outside the visible area of the list.

When you set the cursor, you must take into account the header lines of the list.

REPORT demo_list_set_cursor_3 LINE-SIZE 30
                              NO STANDARD PAGE HEADING.

DATA: inp(2) TYPE c, top(2) TYPE c.

SET PF-STATUS 'LINE_NUMBER'.

DO 50 TIMES.
  WRITE: / 'Line ', (2) sy-index.
ENDDO.

TOP-OF-PAGE.
  WRITE: 'Line number:', inp INPUT ON.
  ULINE.
  SKIP.

AT USER-COMMAND.
  DESCRIBE LIST PAGE 1 TOP-LINES top.
  CASE sy-ucomm.
    WHEN 'LINE'.
      READ LINE 1 FIELD VALUE inp.
      SCROLL LIST TO PAGE 1 LINE inp.
      inp = inp + top.
      SET CURSOR LINE inp OFFSET 6.
  ENDCASE.

This program creates a basic list with an input field and a set of lines.

In the status LINE_NUMBER, the function code LINE (text LINE NUMBER) is assigned to function key F5 and to a pushbutton of the application toolbar. If the user enters a number into the input field and chooses LINE NUMBER, the system scrolls to the specified number and sets the cursor to it:

The system reads the input field using READ LINE. For setting the cursor, it uses DESCRIBE LIST to take into account the size of the page header. Note that this example makes excessive use of automatic type conversion.