Qt5.1 TextField - Keys attached property - capturing key-presses
With the following code:
@import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
title: qsTr("Some App")
Keys.onPressed: res.text = res.text == "" ? "Rect" : "";
text: "Some Text"
Keys.onPressed: res.text = res.text == "" ? "TextField" : "";
Keys.onReturnPressed: res.text = res.text == "" ? "TextField" : "";
Keys.onSpacePressed: res.text = res.text == "" ? "TextField" : "";
It seems the attached property for TextField even with "Keys.priority: Keys.BeforeItem":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-keys.html#priority-prop does not seem to trigger the events for onPressed whereas a Rectangle or Item do so just fine.
Also OnReturnPressed / onReleased works just fine on TextField. (in onReleased however, setting event.accepted seems to have no effect as the text still appears in the TextField indicating either the Keys.onReleased got called after the TextField handlers or the event.accepted = true; was ignored)
This kinda suggests, the events are getting handled internally and thus getting accepted thereby not triggering the onPressed for any standard keys which get handled by the TextField. "Enter" is rightfully so not handled and thereby propagates up to our capture event.
The documentation for "Keys.priority":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-keys.html#priority-prop states that
bq. This property determines whether the keys are processed before or after the attached item's own key handling.
so why is specifying "Keys.BeforeItem" not acting as expected? Is there anything additional that's needed to capture all key-events in a TextField?
Threads like "This":http://developer.nokia.com/Community/Discussion/showthread.php/232027-quot-Enter-quot-key-on-the-TextArea-in-QML state what I'm trying should work fine on a TextField. However seeing it's from 2011 not sure if it even applies or if I'm just missing something.
I have noticed the same thing. Did you find out how to intercept keypresses from TextField?
Nope, Currently this does not seem to be possible with TextField. I have a bug submitted for this. Seems @Jens is currently assigned to it and he seems to prefer TextField having this functionality as well. Not sure if there is any progress on the issue. You can find the issue "Here":https://bugreports.qt-project.org/browse/QTBUG-34101