27

I have a QtQuick project for Desktop. It is very simple:

// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Rectangle {
    width: 360
    height: 360
    Grid
    {
        id: xGrid
        width: parent.width
        height: parent.height
        columns: 2
        spacing: 1

        Rectangle
        {
            height: parent.height
            width: 10
            color: "#ff0000"
            Text {
                id: xText
                text: qsTr("t\na\ns")
            }
        }
        TextEdit
        {
            id: xTextEdit
            height: parent.height
            width: 350
            Keys.onEnterPressed: {
                console.log(event.key)
                xText.text = (qsTr("A"))
            }
        }
    }
}

My code does not run like I want. The Keys.onEnterPressed seem never be captured, so I try Keys.onPressed it work but not sure why when I press Enter, the even.key returns 16777220.

Any one get this issue? How can I solve it?
Thanks for your answer!

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
nvcnvn
  • 4,991
  • 8
  • 49
  • 77

5 Answers5

29

I got the same problem with a TextInput item. I tried

  • onPressed
  • onEnterPressed
  • onReturnPressed

Only the latter one worked (onReturnPressed). I guess, the underlying implementation of the TextInput captures the 'Enter' key so it doesn't get processed by the onPressed signal in a regular way.

By the way: the key code is correct. It's an abstraction on the platform specific key codes.

Pierre GM
  • 19,809
  • 3
  • 56
  • 67
Scharenberg
  • 471
  • 5
  • 6
  • 1
    > I guess, the underlying implementation of the TextInput captures the 'Enter' key so it doesn't get processed by the onPressed signal in a regular way. Your code should be processed first by default. See [Keys.priority](http://qt-project.org/doc/qt-5/qml-qtquick-keys.html#priority-prop) – drzymala Jan 22 '14 at 09:41
  • 1
    @drzymala Keys.onEnterPressed should be called when you press on the enter key *from the numeric keypad* on the right side of the keyboard. That's one this one is for. – mchiasson Feb 12 '19 at 14:26
5

A better way to handle users entering a text value is to use TextInput.onAccepted

Here's an example:

TextInput {
    onAccepted: processText()
}

When the user presses Enter, the processText() method will be called. This approach is simpler and should improve cross-platform portability.

Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
4
TextArea {
id: messageField
Layout.fillWidth: true
placeholderText: qsTr("Message")
wrapMode: TextArea.Wrap
inputMethodHints: Qt.ImhNoPredictiveText

function _onEnterPressed(event)
{
    if ((event.modifiers & Qt.ControlModifier))
    {
        sendMessage()
    }
    else
    {
        event.accepted = false;
    }
}

Keys.onReturnPressed: { _onEnterPressed(event) }
Keys.onEnterPressed: { _onEnterPressed(event) }
}
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
0

Potentially relevant context taken from the docs:

[...] 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
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
0

I'd say use onReturnPressed as well. Otherwise you can also check the key value in onPressed() and react there. onReturn/EnterPressed are just convenience functions.

mauricek
  • 36
  • 2