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
39 signal powerKeyLongPressed;
40 signal volumeDownTriggered;
41 signal volumeUpTriggered;
42 signal screenshotTriggered;
44 readonly property bool altTabPressed: d.altTabPressed
46 property int powerKeyLongPressTime: 2000
48 // For testing. If running windowed (e.g. tryShell), Alt+Tab is taken by the
49 // running desktop, set this to true to use Ctrl+Tab instead.
50 property bool controlInsteadOfAlt: false
55 property bool volumeDownKeyPressed: false
56 property bool volumeUpKeyPressed: false
57 property bool ignoreVolumeEvents: false
59 property bool altPressed: false
60 property bool altTabPressed: false
64 id: powerKeyLongPressTimer
65 interval: root.powerKeyLongPressTime
66 onTriggered: root.powerKeyLongPressed();
69 function onKeyPressed(event) {
70 if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
71 && !event.isAutoRepeat) {
73 // FIXME: We only consider power key presses if the screen is
74 // on because of bugs 1410830/1409003. The theory is that when
75 // those bugs are encountered, there is a >2s delay between the
76 // power press event and the power release event, which causes
77 // the shutdown dialog to appear on resume. So to avoid that
78 // symptom while we investigate the root cause, we simply won't
79 // initiate any dialogs when the screen is off.
80 if (Powerd.status === Powerd.On) {
81 powerKeyLongPressTimer.restart();
83 event.accepted = true;
84 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay)
85 && !event.isAutoRepeat) {
86 event.accepted = callManager.handleMediaKey(false);
87 } else if (event.key == Qt.Key_VolumeDown) {
88 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
89 else if (!event.isAutoRepeat) {
90 if (d.volumeUpKeyPressed) {
91 if (Powerd.status === Powerd.On) root.screenshotTriggered();
92 d.ignoreVolumeEvents = true;
94 d.volumeDownKeyPressed = true;
96 } else if (event.key == Qt.Key_VolumeUp) {
97 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
98 else if (!event.isAutoRepeat) {
99 if (d.volumeDownKeyPressed) {
100 if (Powerd.status === Powerd.On) root.screenshotTriggered();
101 d.ignoreVolumeEvents = true;
103 d.volumeUpKeyPressed = true;
105 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
107 } else if (event.key == Qt.Key_Tab) {
108 if (d.altPressed && !d.altTabPressed) {
109 d.altTabPressed = true;
110 event.accepted = true;
112 } else if (event.key == Qt.Key_Print) {
113 root.screenshotTriggered();
117 function onKeyReleased(event) {
118 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
119 powerKeyLongPressTimer.stop();
120 event.accepted = true;
121 } else if (event.key == Qt.Key_VolumeDown) {
122 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
123 d.volumeDownKeyPressed = false;
124 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
125 } else if (event.key == Qt.Key_VolumeUp) {
126 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
127 d.volumeUpKeyPressed = false;
128 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
129 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
130 d.altPressed = false;
131 if (d.altTabPressed) {
132 d.altTabPressed = false;
133 event.accepted = true;