2 * Copyright (C) 2014-2015 Canonical, Ltd.
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.
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.
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/>.
21 \brief A mapper for the physical keys on the device
23 A mapper to handle events triggered by pressing physical keys on a device.
29 This allows for handling the following events
31 * Volume Decreases/Increases
38 signal powerKeyLongPressed;
39 signal volumeDownTriggered;
40 signal volumeUpTriggered;
41 signal screenshotTriggered;
43 readonly property bool altTabPressed: d.altTabPressed
45 property int powerKeyLongPressTime: 2000
47 // For testing. If running windowed (e.g. tryShell), Alt+Tab is taken by the
48 // running desktop, set this to true to use Ctrl+Tab instead.
49 property bool controlInsteadOfAlt: false
54 property bool volumeDownKeyPressed: false
55 property bool volumeUpKeyPressed: false
56 property bool ignoreVolumeEvents: false
58 property bool altPressed: false
59 property bool altTabPressed: false
63 id: powerKeyLongPressTimer
64 interval: root.powerKeyLongPressTime
65 onTriggered: root.powerKeyLongPressed();
68 function onKeyPressed(event) {
69 if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
70 && (!event.isAutoRepeat || !powerKeyLongPressTimer.running)) {
72 // FIXME: We only consider power key presses if the screen is
73 // on because of bugs 1410830/1409003. The theory is that when
74 // those bugs are encountered, there is a >2s delay between the
75 // power press event and the power release event, which causes
76 // the shutdown dialog to appear on resume. So to avoid that
77 // symptom while we investigate the root cause, we simply won't
78 // initiate any dialogs when the screen is off.
79 if (Powerd.status === Powerd.On) {
80 powerKeyLongPressTimer.restart();
82 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay) && !event.isAutoRepeat) {
83 event.accepted = callManager.handleMediaKey(false);
84 } else if (event.key == Qt.Key_VolumeDown) {
85 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
86 else if (!event.isAutoRepeat) {
87 if (d.volumeUpKeyPressed) {
88 if (Powerd.status === Powerd.On) {
89 root.screenshotTriggered();
91 d.ignoreVolumeEvents = true;
93 d.volumeDownKeyPressed = true;
95 } else if (event.key == Qt.Key_VolumeUp) {
96 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
97 else if (!event.isAutoRepeat) {
98 if (d.volumeDownKeyPressed) {
99 if (Powerd.status === Powerd.On) {
100 root.screenshotTriggered();
102 d.ignoreVolumeEvents = true;
104 d.volumeUpKeyPressed = true;
106 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
108 } else if (event.key == Qt.Key_Tab) {
109 if (d.altPressed && !d.altTabPressed) {
110 d.altTabPressed = true;
111 event.accepted = true;
116 function onKeyReleased(event) {
117 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
118 powerKeyLongPressTimer.stop();
119 event.accepted = true;
120 } else if (event.key == Qt.Key_VolumeDown) {
121 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
122 d.volumeDownKeyPressed = false;
123 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
124 } else if (event.key == Qt.Key_VolumeUp) {
125 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
126 d.volumeUpKeyPressed = false;
127 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
128 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
129 d.altPressed = false;
130 if (d.altTabPressed) {
131 d.altTabPressed = false;
132 event.accepted = true;