Keys QML Type

Provides key handling to Items. 更多...

import 语句: import QtQuick

特性

信号

详细描述

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.

用法范例

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: (event)=> {
        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")
}
					

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.

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. onPressed, onReleased 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. onPressed, onReleased handlers
  5. parent item

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

另请参阅 KeyEvent and KeyNavigation attached property .

特性文档编制

enabled : bool

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

forwardTo : list < Item > [read-only]

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
}
					

To see the order in which events are received when using forwardTo, see Key Handling Priorities .

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.

另请参阅 Key Handling Priorities .

信号文档编制

asteriskPressed ( KeyEvent event )

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

注意: 相应处理程序是 onAsteriskPressed .

backPressed ( KeyEvent event )

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

注意: 相应处理程序是 onBackPressed .

backtabPressed ( KeyEvent event )

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

注意: 相应处理程序是 onBacktabPressed .

callPressed ( KeyEvent event )

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

注意: 相应处理程序是 onCallPressed .

cancelPressed ( KeyEvent event )

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

注意: 相应处理程序是 onCancelPressed .

context1Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onContext1Pressed .

context2Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onContext2Pressed .

context3Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onContext3Pressed .

context4Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onContext4Pressed .

deletePressed ( KeyEvent event )

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

注意: 相应处理程序是 onDeletePressed .

digit0Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit0Pressed .

digit1Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit1Pressed .

digit2Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit2Pressed .

digit3Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit3Pressed .

digit4Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit4Pressed .

digit5Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit5Pressed .

digit6Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit6Pressed .

digit7Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit7Pressed .

digit8Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit8Pressed .

digit9Pressed ( KeyEvent event )

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

注意: 相应处理程序是 onDigit9Pressed .

downPressed ( KeyEvent event )

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

注意: 相应处理程序是 onDownPressed .

enterPressed ( KeyEvent event )

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

注意: 相应处理程序是 onEnterPressed .

escapePressed ( KeyEvent event )

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

注意: 相应处理程序是 onEscapePressed .

flipPressed ( KeyEvent event )

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

注意: 相应处理程序是 onFlipPressed .

hangupPressed ( KeyEvent event )

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

注意: 相应处理程序是 onHangupPressed .

leftPressed ( KeyEvent event )

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

注意: 相应处理程序是 onLeftPressed .

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

注意: 相应处理程序是 onMenuPressed .

noPressed ( KeyEvent event )

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

注意: 相应处理程序是 onNoPressed .

pressed ( KeyEvent event )

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

注意: 相应处理程序是 onPressed .

released ( KeyEvent event )

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

注意: 相应处理程序是 onReleased .

returnPressed ( KeyEvent event )

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

注意: 相应处理程序是 onReturnPressed .

rightPressed ( KeyEvent event )

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

注意: 相应处理程序是 onRightPressed .

selectPressed ( KeyEvent event )

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

注意: 相应处理程序是 onSelectPressed .

shortcutOverride ( KeyEvent event )

This signal is emitted when a key has been pressed that could potentially be used as a shortcut. The event parameter provides information about the event.

Set event.accepted to true if you wish to prevent the pressed key from being used as a shortcut by other types, such as Shortcut 。例如:

Item {
    id: escapeItem
    focus: true
    // Ensure that we get escape key press events first.
    Keys.onShortcutOverride: (event)=> event.accepted = (event.key === Qt.Key_Escape)
    Keys.onEscapePressed: {
        console.log("escapeItem is handling escape");
        // event.accepted is set to true by default for the specific key handlers
    }
}
Shortcut {
    sequence: "Escape"
    onActivated: console.log("Shortcut is handling escape")
}
					

As with the other signals, shortcutOverride will only be emitted for an item if that item has activeFocus .

注意: 相应处理程序是 onShortcutOverride .

另请参阅 Shortcut .

spacePressed ( KeyEvent event )

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

注意: 相应处理程序是 onSpacePressed .

tabPressed ( KeyEvent event )

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

注意: 相应处理程序是 onTabPressed .

upPressed ( KeyEvent event )

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

注意: 相应处理程序是 onUpPressed .

volumeDownPressed ( KeyEvent event )

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

注意: 相应处理程序是 onVolumeDownPressed .

volumeUpPressed ( KeyEvent event )

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

注意: 相应处理程序是 onVolumeUpPressed .

yesPressed ( KeyEvent event )

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

注意: 相应处理程序是 onYesPressed .