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
61 property var powerButtonPressStart: 0
64 function onKeyPressed(event, currentEventTimestamp) {
65 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
66 if (event.isAutoRepeat) {
67 if (d.powerButtonPressStart > 0
68 && currentEventTimestamp - d.powerButtonPressStart >= powerKeyLongPressTime) {
69 d.powerButtonPressStart = 0;
70 root.powerKeyLongPressed();
73 d.powerButtonPressStart = currentEventTimestamp;
75 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay) && !event.isAutoRepeat) {
76 event.accepted = callManager.handleMediaKey(false);
77 } else if (event.key == Qt.Key_VolumeDown) {
78 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
79 else if (!event.isAutoRepeat) {
80 if (d.volumeUpKeyPressed) {
81 if (Powerd.status === Powerd.On) {
82 root.screenshotTriggered();
84 d.ignoreVolumeEvents = true;
86 d.volumeDownKeyPressed = true;
88 } else if (event.key == Qt.Key_VolumeUp) {
89 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
90 else if (!event.isAutoRepeat) {
91 if (d.volumeDownKeyPressed) {
92 if (Powerd.status === Powerd.On) {
93 root.screenshotTriggered();
95 d.ignoreVolumeEvents = true;
97 d.volumeUpKeyPressed = true;
99 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
101 } else if (event.key == Qt.Key_Tab) {
102 if (d.altPressed && !d.altTabPressed) {
103 d.altTabPressed = true;
104 event.accepted = true;
109 function onKeyReleased(event, currentEventTimestamp) {
110 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
111 d.powerButtonPressStart = 0;
112 event.accepted = true;
113 } else if (event.key == Qt.Key_VolumeDown) {
114 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
115 d.volumeDownKeyPressed = false;
116 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
117 } else if (event.key == Qt.Key_VolumeUp) {
118 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
119 d.volumeUpKeyPressed = false;
120 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
121 } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
122 d.altPressed = false;
123 if (d.altTabPressed) {
124 d.altTabPressed = false;
125 event.accepted = true;