Step 1 - Use keyboard keys to navigate between Page nodes

In this step of the tutorial you create keyboard navigation between the Home, Media, Car, and Navigation application screens. You set focus to the toggle buttons on the navigation bar and set the application to navigate between the application screens when the user presses the (Right Arrow) and (Left Arrow) keyboard keys.

Assets for the tutorial

The starting point of this tutorial is the Keyboard input.kzproj Kanzi Studio project file stored in <KanziWorkspace>/Tutorials/Keyboard input/Start directory.

The <KanziWorkspace>/Tutorials/Keyboard input/Completed directory contains the completed project of this tutorial.

The starting point project contains the content you need to complete this tutorial:

Create keyboard navigation between application screens

In this section you create keyboard navigation for the toggle buttons you use to navigate between the Page nodes which show the application screens.

To create keyboard navigation between application screens:

  1. In the Project select the Screen node, in the Node Components right-click in the Triggers, and select Add Trigger > General > On Attached.
    When you start the application, Kanzi by default sets the focus to the Screen node. You use the On Attached trigger to execute a script which sets focus to the navigation bar when you start the application.
  2. In the Node Components add to the On Attached trigger an Execute Script action.
  3. In the Execute Script window select + Create Script, and name the script InitialFocus.
  4. In the Script Editor use this script:
    // Get the Toggle Button Group 2D node NavigationButtons.
    var navigationButtons = node.lookupNode('#NavigationButtons');
    
    // Set focus to the NavigationButtons node.
    navigationButtons.trySetFocus();

    When you are done writing the script, in the Script Editor click Save. You use the script to set focus to the NavigationButtons node when the application starts.

  5. In the Project > RootPage select the NavigationButtons node, in the Properties right-click, select Add Property > Focus Manager > Focus Scope property, and enable that property.
    You use the Focus Scope property to forward the focus from the toggle button group to the toggle buttons which you use to navigate between the application screens. This way the user can use keyboard keys to navigate between those toggle buttons.
  6. In the Project > RootPage > NavigationButtons > Grid Layout 2D select the HomeButton node, in the Properties right-click, select Add Property > Node > Logical Focus property, and enable that property.
    You set the HomeButton node to receive focus when you set focus to the Toggle Button Group 2D node NavigationButtons.
  7. In the Project > RootPage > NavigationButtons > Grid Layout 2D select the HomeButton node, in the Properties right-click, select Add Property > Focus Manager > Right Navigation Node property, and set it to #MediaButton.
    The focus chain in Kanzi enables you to navigate between nodes based on their rendered position using the (Left Arrow), (Right Arrow), (Up Arrow), and (Down Arrow) keys. You use the Right Navigation Node property to set focus from the HomeButton node to the MediaButton node when the user presses the (Right Arrow) key on their keyboard.

    TIP

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

  8. Repeat the previous step for the MediaButton and CarButton nodes but in the Properties set the Right Navigation Node property:
  9. In the Project > RootPage > NavigationButtons > Grid Layout 2D select the MediaButton node, in the Properties right-click, select Add Property > Focus Manager > Left Navigation Node property, and set it to #HomeButton.
    You use the Left Navigation Node property to set focus from the MediaButton node to the HomeButton node when the user presses the (Left Arrow) key on their keyboard.
  10. Repeat the previous step for the CarButton and NavigationButton nodes but in the Properties set the Left Navigation Node property:

Restart the Preview to set the initial focus to the HomeButton toggle button. You can now use the (Right Arrow) and (Left Arrow) keyboard keys to navigate between application screens.


< INTRODUCTION
NEXT STEP >

See also

Using the Page and Page Host nodes

Using triggers

Using scripts

Scripting reference