on xenial help sign in
Avoid volume control changes when both volume keys are pressed.
8c22a78Alberto Aguirre
qml/Components/ScreenGrabber.qml+6−33
@@ -26,14 +26,6 @@ Rectangle {
26 anchors.fill: parent
26 anchors.fill: parent
27 opacity: 0.0
27 opacity: 0.0
28
28
29−
30− QtObject {
31− id: keyState
32− property bool volumeUpPressed: false
33− property bool volumeDownPressed: false
34− property bool ignoreKeyRepeats: false
35− }
36−
37 ScreenGrabber {
29 ScreenGrabber {
38 id: screenGrabber
30 id: screenGrabber
39 objectName: "screenGrabber"
31 objectName: "screenGrabber"
@@ -45,33 +37,14 @@ Rectangle {
45 source: "/system/media/audio/ui/camera_click.ogg"
37 source: "/system/media/audio/ui/camera_click.ogg"
46 }
38 }
47
39
48− function onKeyPressed(key) {
40+ function capture() {
49− if (!enabled || keyState.ignoreKeyRepeats)
41+ if (!enabled)
50 return;
42 return;
51
43
52− if (key == Qt.Key_VolumeUp)
53− keyState.volumeUpPressed = true;
54− else if (key == Qt.Key_VolumeDown)
55− keyState.volumeDownPressed = true;
56−
57− if (keyState.volumeDownPressed && keyState.volumeUpPressed) {
58− // Only take one screenshot if both keys are held
59− keyState.ignoreKeyRepeats = true;
60− visible = true;
44+ visible = true;
61− shutterSound.stop();
45+ shutterSound.stop();
62− shutterSound.play();
46+ shutterSound.play();
63− fadeIn.start();
47+ fadeIn.start();
64− }
65− }
66−
67− function onKeyReleased(key) {
68− if (key == Qt.Key_VolumeUp) {
69− keyState.volumeUpPressed = false;
70− keyState.ignoreKeyRepeats = false;
71− } else if (key == Qt.Key_VolumeDown) {
72− keyState.volumeDownPressed = false;
73− keyState.ignoreKeyRepeats = false;
74− }
75 }
48 }
76
49
77 NumberAnimation on opacity {
50 NumberAnimation on opacity {
qml/Components/VolumeKeyFilter.qmlnew file+63
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.0
18+/*!
19+ \brief A filter for volume keys
20+
21+A filter which treats volume keys as single tri-state key with the states:
22+VolumeUp Pressed, VolumeDown Pressed or Volume Up+Down pressed
23+*/
24+QtObject {
25+ id: root
26+
27+ signal volumeUpPressed()
28+ signal volumeDownPressed()
29+ signal bothVolumeKeysPressed()
30+
31+ property bool volumeUpKeyPressed: false
32+ property bool volumeDownKeyPressed: false
33+ property bool aVolumeKeyWasReleased: true
34+
35+ function onKeyPressed(key) {
36+ if (key == Qt.Key_VolumeUp)
37+ volumeUpKeyPressed = true;
38+ else if (key == Qt.Key_VolumeDown)
39+ volumeDownKeyPressed = true;
40+
41+ if (volumeDownKeyPressed && volumeUpKeyPressed) {
42+ //avoids sending a signal repeatedly if both keys are held
43+ //instead one of the keys must have been previously released
44+ if (aVolumeKeyWasReleased)
45+ bothVolumeKeysPressed();
46+ aVolumeKeyWasReleased = false;
47+ } else if (volumeDownKeyPressed) {
48+ volumeDownPressed();
49+ } else if (volumeUpKeyPressed) {
50+ volumeUpPressed();
51+ }
52+ }
53+
54+ function onKeyReleased(key) {
55+ if (key == Qt.Key_VolumeUp) {
56+ volumeUpKeyPressed = false;
57+ aVolumeKeyWasReleased = true;
58+ } else if (key == Qt.Key_VolumeDown) {
59+ volumeDownKeyPressed = false;
60+ aVolumeKeyWasReleased = true;
61+ }
62+ }
63+}
qml/Shell.qml+8−6
@@ -144,28 +144,30 @@ Item {
144 value: launcher.shown || launcher.dashSwipe
144 value: launcher.shown || launcher.dashSwipe
145 }
145 }
146
146
147+ VolumeKeyFilter {
148+ id: volumeKeyFilter
149+ onVolumeDownPressed: volumeControl.volumeDown()
150+ onVolumeUpPressed: volumeControl.volumeUp()
151+ onBothVolumeKeysPressed: screenGrabber.capture()
152+ }
147
153
148 WindowKeysFilter {
154 WindowKeysFilter {
149− // Handle but do not filter out volume keys
150− Keys.onVolumeUpPressed: { volumeControl.volumeUp(); event.accepted = false; }
151− Keys.onVolumeDownPressed: { volumeControl.volumeDown(); event.accepted = false; }
152−
153 Keys.onPressed: {
155 Keys.onPressed: {
154− screenGrabber.onKeyPressed(event.key);
155 if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
156 if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
156 dialogs.onPowerKeyPressed();
157 dialogs.onPowerKeyPressed();
157 event.accepted = true;
158 event.accepted = true;
158 } else {
159 } else {
160+ volumeKeyFilter.onKeyPressed(event.key);
159 event.accepted = false;
161 event.accepted = false;
160 }
162 }
161 }
163 }
162
164
163 Keys.onReleased: {
165 Keys.onReleased: {
164− screenGrabber.onKeyReleased(event.key);
165 if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
166 if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
166 dialogs.onPowerKeyReleased();
167 dialogs.onPowerKeyReleased();
167 event.accepted = true;
168 event.accepted = true;
168 } else {
169 } else {
170+ volumeKeyFilter.onKeyReleased(event.key);
169 event.accepted = false;
171 event.accepted = false;
170 }
172 }
171 }
173 }