Using focus

To handle focus in your Kanzi application, you can:

Default keyboard navigation keys

These are the default keyboard keys you can use to navigate the focus in your application:

Setting focus

By default the Screen node has focus. The user can interact with user interface elements using keyboard keys when those elements have focus.

To set focus:

  1. In the Project select the node to which you want to set focus and in the Node Components add a trigger which you use to set off the action that sets the focus.
    For example, add a Page Activated trigger. The Page Activated trigger is set off when a Page or Page Host node becomes active.
  2. In the Node Components add to the Page Activated trigger an Execute Script action.
    You set the application to execute a JavaScript script when the Page or Page Host node to which you added the trigger becomes active.
  3. In the Execute Script window select + Create Script to create a script.
  4. In the Script Editor use the trySetFocus() function to set focus to the node in the project.
    See Scripting reference.
    For example, to set focus to a Slider 2D node in the project use this script:
    // Get the Slider 2D node to which you want to set focus.
    slider = node.lookupNode('#Slider 2D');
    
    // Set focus to the Slider 2D node.
    slider.trySetFocus();

When you navigate to the page for which you added the trigger, you set focus to the node you defined in the script. For example, if you set focus to a Slider node, you can by default use the arrow keys on your keyboard to move the slider.

Showing when a user interface element has focus

You can set a user interface element, such as a button or a slider, to change appearance when it has focus. For example, you can highlight a button when that button has focus or change how a slider knob looks when the slider has focus.

To show when a user interface element has focus:

  1. In the Project select the node for which you want to create focus visualization and in the State Tools click Create State Manager to create a state manager for that node.

  2. In the State Tools click Create State twice to create two states. Name them, for example, NotFocused and Focused.
    The NotFocused state defines the state of your application when the node is not focused and the Focused state when the node is focused.

  3. In the Project select the node for which you want to create focus visualization and set the value of the property you want to change when that node has focus.
    For example, you can use a different image for the slider knob when the slider has focus.
  4. In the State Tools click below the state which defines how the node looks when it has focus to save the current property values to that state.
    For example, click below the Focused state.

  5. In the State Tools click the <No Controller Property> dropdown menu and select the Node > Focused property.
    When you set the Focused property as the controller property for the state manager, the state manager transitions to a state based on the value of that property.

  6. In the State Tools under the state which defines how the node looks when it has focus, set the value of the Focused property to True.

  7. In the State Tools click Edit State Manager to deactivate the State Tools.

When you set focus to the slider, the appearance of the slider knob changes.

Moving focus in a focus chain

Focus chain is a sequence of nodes which defines the order in which Kanzi sets focus to those nodes. Kanzi automatically includes in the focus chain all nodes in your project. By default the focus chain moves the active focus from each node to the next child node. When a node has no child node which can receive focus, the focus moves to the next node in the project tree. You can define a custom focus chain or redirect the default focus chain navigation by setting which node receives focus after the currently focused node.

To move focus in a focus chain:

  1. In the Project select each node which you want to receive focus and in the Properties add and set:

    You can move the focus to the next node in the focus chain by pressing the Tab key and to the previous node by pressing the Shift Tab keys.

    TIP

    Aliases provide a convenient way to set the nodes in a focus chain.

  2. In the Project select each node in your project which you want to not receive focus and in the Properties right-click, select Add Property > Node > Focusable property, and disable that property.


Setting a focus scope

Use a focus scope to handle navigation in a focus chain. You can set a node to be a focus scope to forward the focus from that node to a child node. When you set focus to a focus scope, Kanzi forwards the focus to the child node of that scope which has logical focus.

To set a focus scope:

  1. In the Project select the node which you want to set as a focus scope and in the Properties add and enable the Focus Scope property.
  2. In the Project select the child node of the focus scope node to which you want to set focus and in the Properties add and enable the Logical Focus property.
    When you set focus to the focus scope node, the child node for which you enable the Logical Focus property receives focus. If the focus scope has several child nodes with the Logical Focus property enabled, the focus manager sets focus to the last child node.

Setting a focus fence

Use a focus fence to navigate the focus within a part of an application. A focus fence allows the user to navigate the content within the boundaries of a focus scope. For example, you can use a focus fence to create keyboard navigation for a navigation bar or a popup window.

To set a focus fence:

  1. In the Project select the node which you want to set as a focus fence and set that node as a focus scope. See Setting a focus scope.
  2. In the Project select the node you set as a focus scope, in the Properties add the Focusable property, and disable the property.
    When you set a focus scope to not receive focus, you cannot navigate to or from the focus scope boundaries.

Setting a specific keyboard key to move focus

When creating keyboard navigation for your application, you can set which keyboard keys the user can use to navigate the application.

To set a specific keyboard key to move focus:

  1. In the Project select the node from which you want to move focus to another node with a specific keyboard key.
  2. In the Node Components right-click in the Triggers and add the Keyboard > Key Down trigger.
    You use the Key Down trigger to execute a script which sets focus to a node when the user presses a specific key on the keyboard.

  3. In the Node Components click Trigger Settings and in the Trigger Settings Editor click Add condition. The Trigger Condition Editor opens.
    Trigger conditions enable you to set which conditions must be met for the trigger to set off. You set off the Key Down trigger when the node to which you added that trigger has focus and the user presses a specific key on the keyboard.
  4. In the Trigger Condition Editor set which keyboard key sets off the Key Down trigger:


    In the Trigger Condition Editor and the Trigger Settings Editor windows click Save.

    When the user presses down the E key, the Key Down trigger executes the script which sets the focus.

  5. In the Node Components add to the Key Down trigger an Execute Script action.
  6. In the Execute Script window select + Create Script to create a script.

  7. In the Script Editor use a scripting function to set focus to the node in the project.
    For example, use the tryMoveActiveFocusForward() function to set focus to the next node in the focus chain use this script. See Scripting reference and Focus.
    // Sets focus to the next node in the focus chain.
    tryMoveActiveFocusForward();

Using focus in the API

For details, see the FocusManager class in the API reference.

See also

Focus

Tutorial: Use keyboard input to navigate your application

Using scripts

Scripting reference

Using state managers