From 0df696bcdc3cd17a517e442530eb8c1718ebbbf8 Mon Sep 17 00:00:00 2001 From: Adriano Rezende Date: Tue, 3 Jul 2012 18:41:35 +0200 Subject: Handle key press and release correctly for buttons The clicked signal should not be emitted multiple times when the button is just pressed. Change-Id: I3f1f3ab7f135b366150af985b41409afa0130568 Reviewed-by: Leonardo Sobral Cunha Reviewed-by: Jens Bache-Wiig --- components/Button.qml | 2 -- components/private/BasicButton.qml | 20 +++++++++++--------- components/private/ButtonBehavior.qml | 1 + 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/components/Button.qml b/components/Button.qml index 10908b7f..4ae591d1 100644 --- a/components/Button.qml +++ b/components/Button.qml @@ -52,8 +52,6 @@ BasicButton { Accessible.name: text - Keys.onSpacePressed:animateClick() - delegate: StyleItem { id: styleitem anchors.fill: parent diff --git a/components/private/BasicButton.qml b/components/private/BasicButton.qml index daa17adf..e5509661 100644 --- a/components/private/BasicButton.qml +++ b/components/private/BasicButton.qml @@ -67,16 +67,18 @@ Item { implicitWidth: loader.item.implicitWidth implicitHeight: loader.item.implicitHeight - function animateClick() { - behavior.keyPressed = true - button.clicked() - animateClickTimer.start() + Keys.onPressed: { + if (event.key == Qt.Key_Space && !event.isAutoRepeat && !behavior.pressed) + behavior.keyPressed = true; } - Timer { - id: animateClickTimer - interval: 250 - onTriggered: behavior.keyPressed = false + Keys.onReleased: { + if (event.key == Qt.Key_Space && !event.isAutoRepeat && behavior.keyPressed) { + behavior.keyPressed = false; + if (checkable) + checked = !checked; + button.clicked(); + } } Loader { @@ -93,7 +95,7 @@ Item { onClicked: button.clicked() onExited: Internal.hideToolTip() onCanceled: Internal.hideToolTip() - onPressed: if (activeFocusOnPress) button.focus = true + onPressed: if (activeFocusOnPress) button.forceActiveFocus() Timer { interval: 1000 diff --git a/components/private/ButtonBehavior.qml b/components/private/ButtonBehavior.qml index fdf0c79c..c8f010c9 100644 --- a/components/private/ButtonBehavior.qml +++ b/components/private/ButtonBehavior.qml @@ -47,6 +47,7 @@ MouseArea { property bool effectivePressed: pressed && containsMouse || keyPressed hoverEnabled: true + enabled: !keyPressed onCheckableChanged: { if (!checkable) -- cgit v1.2.1