Unity 8
PhysicalKeysMapper.qml
1 /*
2  * Copyright (C) 2014-2015 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.4
18 import Powerd 0.1
19 
20 /*!
21  \brief A mapper for the physical keys on the device
22 
23  A mapper to handle events triggered by pressing physical keys on a device.
24  Keys included are
25  * Volume Decrease
26  * Volume Increase
27  * Power
28 
29  This allows for handling the following events
30  * Power dialog
31  * Volume Decreases/Increases
32  * Screenshots
33 */
34 
35 Item {
36  id: root
37 
38  signal powerKeyLongPressed;
39  signal volumeDownTriggered;
40  signal volumeUpTriggered;
41  signal screenshotTriggered;
42 
43  readonly property bool altTabPressed: d.altTabPressed
44 
45  property int powerKeyLongPressTime: 2000
46 
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
50 
51  QtObject {
52  id: d
53 
54  property bool volumeDownKeyPressed: false
55  property bool volumeUpKeyPressed: false
56  property bool ignoreVolumeEvents: false
57 
58  property bool altPressed: false
59  property bool altTabPressed: false
60 
61  property var powerButtonPressStart: 0
62  }
63 
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();
71  }
72  } else {
73  d.powerButtonPressStart = currentEventTimestamp;
74  }
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();
83  }
84  d.ignoreVolumeEvents = true;
85  }
86  d.volumeDownKeyPressed = true;
87  }
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();
94  }
95  d.ignoreVolumeEvents = true;
96  }
97  d.volumeUpKeyPressed = true;
98  }
99  } else if (event.key == Qt.Key_Alt || (root.controlInsteadOfAlt && event.key == Qt.Key_Control)) {
100  d.altPressed = true;
101  } else if (event.key == Qt.Key_Tab) {
102  if (d.altPressed && !d.altTabPressed) {
103  d.altTabPressed = true;
104  event.accepted = true;
105  }
106  }
107  }
108 
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;
126  }
127  }
128  }
129 }