on 9636bc0 help sign in
unity8/qml/Components/PhysicalKeysMapper.qml
152 lines 5.5 kB history raw
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 */
17import QtQuick 2.0
18import Powerd 0.1
20/*!
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.
24 Keys included are
25 * Volume Decrease
26 * Volume Increase
27 * Power
29 This allows for handling the following events
30 * Power dialog
31 * Volume Decreases/Increases
32 * Screenshots
34*/
36Item {
37 id: root
39 signal powerKeyLongPressed;
40 signal volumeDownTriggered;
41 signal volumeUpTriggered;
42 signal screenshotTriggered;
44 signal altTabNext();
45 signal altTabPrevious();
46 readonly property bool altTabPressed: d.altTabPressed
48 property int powerKeyLongPressTime: 2000
50 QtObject {
51 id: d
53 property bool volumeDownKeyPressed: false
54 property bool volumeUpKeyPressed: false
55 property bool ignoreVolumeEvents: false
57 property bool altPressed: false
58 property bool altTabPressed: false
59 }
61 Timer {
62 id: powerKeyLongPressTimer
63 interval: root.powerKeyLongPressTime
64 onTriggered: root.powerKeyLongPressed();
65 }
67 function onKeyPressed(event) {
68 if ((event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff)
69 && !event.isAutoRepeat) {
71 // FIXME: We only consider power key presses if the screen is
72 // on because of bugs 1410830/1409003. The theory is that when
73 // those bugs are encountered, there is a >2s delay between the
74 // power press event and the power release event, which causes
75 // the shutdown dialog to appear on resume. So to avoid that
76 // symptom while we investigate the root cause, we simply won't
77 // initiate any dialogs when the screen is off.
78 if (Powerd.status === Powerd.On) {
79 powerKeyLongPressTimer.restart();
80 }
81 event.accepted = true;
82 } else if ((event.key == Qt.Key_MediaTogglePlayPause || event.key == Qt.Key_MediaPlay)
83 && !event.isAutoRepeat) {
84 event.accepted = callManager.handleMediaKey(false);
85 } else if (event.key == Qt.Key_VolumeDown) {
86 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeDownTriggered();
87 else if (!event.isAutoRepeat) {
88 if (d.volumeUpKeyPressed) {
89 if (Powerd.status === Powerd.On) root.screenshotTriggered();
90 d.ignoreVolumeEvents = true;
91 }
92 d.volumeDownKeyPressed = true;
93 }
94 } else if (event.key == Qt.Key_VolumeUp) {
95 if (event.isAutoRepeat && !d.ignoreVolumeEvents) root.volumeUpTriggered();
96 else if (!event.isAutoRepeat) {
97 if (d.volumeDownKeyPressed) {
98 if (Powerd.status === Powerd.On) root.screenshotTriggered();
99 d.ignoreVolumeEvents = true;
100 }
101 d.volumeUpKeyPressed = true;
102 }
103 } else if (event.key == Qt.Key_Control) {
104 print("controll pressed");
105 d.altPressed = true;
106 } else if (event.key == Qt.Key_Tab) {
107 print("tab pressed")
108 if (d.altPressed && !d.altTabPressed) {
109 d.altTabPressed = true;
110 } else if (d.altTabPressed) {
111 print("alttabnext")
112 root.altTabNext();
113 }
114 } else if (event.key == Qt.Key_Backtab) {
115 if (d.altTabPressed) {
116 root.altTabPrevious();
117 }
118 } else if (event.key == Qt.Key_Left) {
119 if (d.altTabPressed) {
120 print("alttabprevious")
121 root.altTabPrevious();
122 event.accpeted = true;
123 }
124 } else if (event.key == Qt.Key_Right) {
125 if (d.altTabPressed) {
126 root.altTabNext();
127 event.accepted = true;
128 }
129 }
130 }
132 function onKeyReleased(event) {
133 if (event.key == Qt.Key_PowerDown || event.key == Qt.Key_PowerOff) {
134 powerKeyLongPressTimer.stop();
135 event.accepted = true;
136 } else if (event.key == Qt.Key_VolumeDown) {
137 if (!d.ignoreVolumeEvents) root.volumeDownTriggered();
138 d.volumeDownKeyPressed = false;
139 if (!d.volumeUpKeyPressed) d.ignoreVolumeEvents = false;
140 } else if (event.key == Qt.Key_VolumeUp) {
141 if (!d.ignoreVolumeEvents) root.volumeUpTriggered();
142 d.volumeUpKeyPressed = false;
143 if (!d.volumeDownKeyPressed) d.ignoreVolumeEvents = false;
144 } else if (event.key == Qt.Key_Control) {
145 d.altPressed = false;
146 if (d.altTabPressed) {
147 d.altTabPressed = false;
148 event.accepted = true;
149 }
150 }
151 }