Qt Reference Documentation

# QML Keys Element

The Keys attached property provides key handling to Items. More...

This element was introduced in Qt 4.7.

- List of all members, including inherited members

## Properties

- enabled : bool
- forwardTo : list<Object>
- priority : enumeration

## Signals

- onAsteriskPressed
- onBackPressed
- onBacktabPressed
- onCallPressed
- onCancelPressed
- onContext1Pressed
- onContext2Pressed
- onContext3Pressed
- onContext4Pressed
- onDeletePressed
- onDigit0Pressed
- onDigit1Pressed
- onDigit2Pressed
- onDigit3Pressed
- onDigit4Pressed
- onDigit5Pressed
- onDigit6Pressed
- onDigit7Pressed
- onDigit8Pressed
- onDigit9Pressed
- onDownPressed
- onEnterPressed
- onEscapePressed
- onFlipPressed
- onHangupPressed
- onLeftPressed
- onMenuPressed
- onNoPressed
- onPressed
- onReleased
- onReturnPressed
- onRightPressed
- onSelectPressed
- onSpacePressed
- onTabPressed
- onUpPressed
- onVolumeDownPressed
- onVolumeUpPressed
- onYesPressed

## Detailed Description

All visual primitives support key handling via the Keys attached property. Keys can be handled via the onPressed and onReleased signal properties.

The signal properties have a KeyEvent parameter, named event which contains details of the event. If a key is handled event.accepted should be set to true to prevent the event from propagating up the item hierarchy.

## Example Usage

The following example shows how the general onPressed handler can be used to test for a certain key; in this case, the left cursor key:

     Item {
         anchors.fill: parent
         focus: true
         Keys.onPressed: {
             if (event.key == Qt.Key_Left) {
                 console.log("move left");
                 event.accepted = true;
             }
         }
     }

Some keys may alternatively be handled via specific signal properties, for example onSelectPressed. These handlers automatically set event.accepted to true.

     Item {
         anchors.fill: parent
         focus: true
         Keys.onLeftPressed: console.log("move left")
     }

See Qt.Key for the list of keyboard codes.

## Key Handling Priorities

The Keys attached property can be configured to handle key events before or after the item it is attached to. This makes it possible to intercept events in order to override an item's default behavior, or act as a fallback for keys not handled by the item.

If priority is Keys.BeforeItem (default) the order of key event processing is:

1. Items specified in `forwardTo`
2. specific key handlers, e.g. onReturnPressed
3. onKeyPress, onKeyRelease handlers
4. Item specific key handling, e.g. TextInput key handling
5. parent item

If priority is Keys.AfterItem the order of key event processing is:

1. Item specific key handling, e.g. TextInput key handling
2. Items specified in `forwardTo`
3. specific key handlers, e.g. onReturnPressed
4. onKeyPress, onKeyRelease handlers
5. parent item

If the event is accepted during any of the above steps, key propagation stops.

See also KeyEvent and KeyNavigation attached property.

## Property Documentation

enabled : bool

This flags enables key handling if true (default); otherwise no key handlers will be called.

read-only forwardTo : list<Object>

This property provides a way to forward key presses, key releases, and keyboard input coming from input methods to other items. This can be useful when you want one item to handle some keys (e.g. the up and down arrow keys), and another item to handle other keys (e.g. the left and right arrow keys). Once an item that has been forwarded keys accepts the event it is no longer forwarded to items later in the list.

This example forwards key events to two lists:

     Item {
         ListView {
             id: list1
             // ...
         }
         ListView {
             id: list2
             // ...
         }
         Keys.forwardTo: [list1, list2]
         focus: true
     }

priority : enumeration

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

- Keys.BeforeItem (default) - process the key events before normal item key processing. If the event is accepted it will not be passed on to the item.
- Keys.AfterItem - process the key events after normal item key handling. If the item accepts the key event it will not be handled by the Keys attached property handler.

## Signal Documentation

Keys::onAsteriskPressed ( KeyEvent event )

This handler is called when the Asterisk '*' has been pressed. The event parameter provides information about the event.

Keys::onBackPressed ( KeyEvent event )

This handler is called when the Back key has been pressed. The event parameter provides information about the event.

Keys::onBacktabPressed ( KeyEvent event )

This handler is called when the Shift+Tab key combination (Backtab) has been pressed. The event parameter provides information about the event.

Keys::onCallPressed ( KeyEvent event )

This handler is called when the Call key has been pressed. The event parameter provides information about the event.

Keys::onCancelPressed ( KeyEvent event )

This handler is called when the Cancel key has been pressed. The event parameter provides information about the event.

Keys::onContext1Pressed ( KeyEvent event )

This handler is called when the Context1 key has been pressed. The event parameter provides information about the event.

Keys::onContext2Pressed ( KeyEvent event )

This handler is called when the Context2 key has been pressed. The event parameter provides information about the event.

Keys::onContext3Pressed ( KeyEvent event )

This handler is called when the Context3 key has been pressed. The event parameter provides information about the event.

Keys::onContext4Pressed ( KeyEvent event )

This handler is called when the Context4 key has been pressed. The event parameter provides information about the event.

Keys::onDeletePressed ( KeyEvent event )

This handler is called when the Delete key has been pressed. The event parameter provides information about the event.

Keys::onDigit0Pressed ( KeyEvent event )

This handler is called when the digit '0' has been pressed. The event parameter provides information about the event.

Keys::onDigit1Pressed ( KeyEvent event )

This handler is called when the digit '1' has been pressed. The event parameter provides information about the event.

Keys::onDigit2Pressed ( KeyEvent event )

This handler is called when the digit '2' has been pressed. The event parameter provides information about the event.

Keys::onDigit3Pressed ( KeyEvent event )

This handler is called when the digit '3' has been pressed. The event parameter provides information about the event.

Keys::onDigit4Pressed ( KeyEvent event )

This handler is called when the digit '4' has been pressed. The event parameter provides information about the event.

Keys::onDigit5Pressed ( KeyEvent event )

This handler is called when the digit '5' has been pressed. The event parameter provides information about the event.

Keys::onDigit6Pressed ( KeyEvent event )

This handler is called when the digit '6' has been pressed. The event parameter provides information about the event.

Keys::onDigit7Pressed ( KeyEvent event )

This handler is called when the digit '7' has been pressed. The event parameter provides information about the event.

Keys::onDigit8Pressed ( KeyEvent event )

This handler is called when the digit '8' has been pressed. The event parameter provides information about the event.

Keys::onDigit9Pressed ( KeyEvent event )

This handler is called when the digit '9' has been pressed. The event parameter provides information about the event.

Keys::onDownPressed ( KeyEvent event )

This handler is called when the Down arrow has been pressed. The event parameter provides information about the event.

Keys::onEnterPressed ( KeyEvent event )

This handler is called when the Enter key has been pressed. The event parameter provides information about the event.

Keys::onEscapePressed ( KeyEvent event )

This handler is called when the Escape key has been pressed. The event parameter provides information about the event.

Keys::onFlipPressed ( KeyEvent event )

This handler is called when the Flip key has been pressed. The event parameter provides information about the event.

Keys::onHangupPressed ( KeyEvent event )

This handler is called when the Hangup key has been pressed. The event parameter provides information about the event.

Keys::onLeftPressed ( KeyEvent event )

This handler is called when the Left arrow has been pressed. The event parameter provides information about the event.

Keys::onMenuPressed ( KeyEvent event )

This handler is called when the Menu key has been pressed. The event parameter provides information about the event.

Keys::onNoPressed ( KeyEvent event )

This handler is called when the No key has been pressed. The event parameter provides information about the event.

Keys::onPressed ( KeyEvent event )

This handler is called when a key has been pressed. The event parameter provides information about the event.

Keys::onReleased ( KeyEvent event )

This handler is called when a key has been released. The event parameter provides information about the event.

Keys::onReturnPressed ( KeyEvent event )

This handler is called when the Return key has been pressed. The event parameter provides information about the event.

Keys::onRightPressed ( KeyEvent event )

This handler is called when the Right arrow has been pressed. The event parameter provides information about the event.

Keys::onSelectPressed ( KeyEvent event )

This handler is called when the Select key has been pressed. The event parameter provides information about the event.

Keys::onSpacePressed ( KeyEvent event )

This handler is called when the Space key has been pressed. The event parameter provides information about the event.

Keys::onTabPressed ( KeyEvent event )

This handler is called when the Tab key has been pressed. The event parameter provides information about the event.

Keys::onUpPressed ( KeyEvent event )

This handler is called when the Up arrow has been pressed. The event parameter provides information about the event.

Keys::onVolumeDownPressed ( KeyEvent event )

This handler is called when the VolumeDown key has been pressed. The event parameter provides information about the event.

Keys::onVolumeUpPressed ( KeyEvent event )

This handler is called when the VolumeUp key has been pressed. The event parameter provides information about the event.

Keys::onYesPressed ( KeyEvent event )

This handler is called when the Yes key has been pressed. The event parameter provides information about the event.