- Qt 6.10
- Qt Quick
- QML Types
- KeyNavigation
- Qt 6.10.2 Reference Documentation

# KeyNavigation QML Type

Supports key navigation by arrow keys. More...

Import Statement: | `import QtQuick`

- List of all members, including inherited members

## Attached Properties

- backtab : Item
- down : Item
- left : Item
- priority : enumeration
- right : Item
- tab : Item
- up : Item

## Detailed Description

Key-based user interfaces commonly allow the use of arrow keys to navigate between focusable items. The KeyNavigation attaching type enables this behavior by providing a convenient way to specify the item that should gain focus when an arrow or tab key is pressed.

The following example provides key navigation for a 2x2 grid of items:

     import QtQuick

     Grid {
         width: 100; height: 100
         columns: 2

         Rectangle {
             id: topLeft
             width: 50; height: 50
             color: focus ? "red" : "lightgray"
             focus: true

             KeyNavigation.right: topRight
             KeyNavigation.down: bottomLeft
         }

         Rectangle {
             id: topRight
             width: 50; height: 50
             color: focus ? "red" : "lightgray"

             KeyNavigation.left: topLeft
             KeyNavigation.down: bottomRight
         }

         Rectangle {
             id: bottomLeft
             width: 50; height: 50
             color: focus ? "red" : "lightgray"

             KeyNavigation.right: bottomRight
             KeyNavigation.up: topLeft
         }

         Rectangle {
             id: bottomRight
             width: 50; height: 50
             color: focus ? "red" : "lightgray"

             KeyNavigation.left: bottomLeft
             KeyNavigation.up: topRight
         }
     }

The top-left item initially receives focus by setting focus to `true`. When an arrow key is pressed, the focus will move to the appropriate item, as defined by the value that has been set for the KeyNavigation left, right, up or down properties.

Note that if a KeyNavigation attaching type receives the key press and release events for a requested arrow or tab key, the event is accepted and does not propagate any further.

By default, KeyNavigation receives key events after the item to which it is attached. If the item accepts the key event, the KeyNavigation attaching type will not receive an event for that key. Setting the priority property to `KeyNavigation.BeforeItem` allows the event to be used for key navigation before the item, rather than after.

If the item to which the focus is switching is not enabled or visible, an attempt will be made to skip this item and focus on the next. This is possible if there are a chain of items with the same KeyNavigation handler. If multiple items in a row are not enabled or visible, they will also be skipped.

KeyNavigation will implicitly set the other direction to return focus to this item. So if you set left to another item, right will be set on that item's KeyNavigation to set focus back to this item. However, if that item's KeyNavigation has had right explicitly set then no change will occur. This means that the example above could achieve the same behavior without specifying KeyNavigation.right or KeyNavigation.down for any of the items.

See also Keys attached property.

## Attached Property Documentation

KeyNavigation.backtab : Item

This property holds the item to assign focus to when the Shift+Tab key combination (Backtab) is pressed.

KeyNavigation.down : Item

This property holds the item to assign focus to when the down cursor key is pressed.

KeyNavigation.left : Item

This property holds the item to assign focus to when the left cursor key is pressed.

KeyNavigation.priority : enumeration

This property determines whether the keys are processed before or after the attached item's own key handling.

Constant | Description
--- | ---
`KeyNavigation.BeforeItem` | process the key events before normal item key processing. If the event is used for key navigation, it will be accepted and will not be passed on to the item.
`KeyNavigation.AfterItem` | (default) process the key events after normal item key handling. If the item accepts the key event it will not be handled by the KeyNavigation attached property handler.

KeyNavigation.right : Item

This property holds the item to assign focus to when the right cursor key is pressed.

KeyNavigation.tab : Item

This property holds the item to assign focus to when the Tab key is pressed.

KeyNavigation.up : Item

This property holds the item to assign focus to when the up cursor key is pressed.