Focus

You can set when user interface elements in your application have focus. Focus allows the user to access the nodes in a Kanzi Studio project and to create keyboard navigation. For example, the user can press the arrow keys on the keyboard to move a slider or press the Enter key to click a button.
See Tutorial: Use keyboard input to navigate your application.

By default the Screen node has focus. The control nodes in Kanzi by default receive input from specific keyboard keys:

Setting 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 Screen node and in the Node Components add a trigger which you use to set off the action that sets the focus.
    For example, add a Key Down trigger to set focus to a node in the project when the user presses a specific key on their keyboard.
  2. 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. Here you set the Key Down trigger to be set off when the user presses a specific key on the keyboard.
  3. 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 (Up Arrow) key, the Key Down trigger executes the actions you set in the trigger. For a list of the keyboard key codes used in Kanzi, see Keyboard input codes reference.

  4. In the Node Components add to the Key Down trigger an Execute Script action.
    You set the application to execute a JavaScript script when the user presses the keyboard key you defined in the trigger.
  5. In the Execute Script window select + Create Script to create a new script.
    The Script Editor window opens.
  6. 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 press the (Up Arrow) key on the keyboard, 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.

Clearing focus

You can clear the focus from the node which currently has focus. For example, if you have user interface elements that show when they have focus, use this function to remove the focus, set controls to the state without focus, and make your application not receive input from the keyboard.

To clear focus:

  1. In the Project select the node from which you want to clear focus and in the Node Components add a trigger which you use to set off the action that clears the focus.
    For example, add an Entered State trigger to clear the focus when a state manager enters a state.

  2. In the Node Components add to the Entered State trigger an Execute Script action.
    You set the application to execute a JavaScript script which clears the focus when the application enters a state.
  3. In the Execute Script window select + Create Script to create a new script.
    The Script Editor window opens.
  4. In the Script Editor use the clearFocus() function which clears the focus from the node which has focus. See Scripting reference.
    // Clear the focus from the currently focused node.
    clearFocus();

When you set off the trigger which executes the script, you clear the focus from the node which currently has focus. Your application does not receive input from the keyboard.

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. When the node has focus, the appearance of that node changes.

  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.

See also

Using scripts

Scripting reference

Creating state managers

Tutorial: Use keyboard input to navigate your application

Using the Button nodes

Using the Toggle Button nodes