on 337d819 help sign in
unity8/qml/UnityInputInfo.qml
70 lines 2.1 kB history raw
1import QtQuick 2.3
2import QtSystemInfo 5.0
4Item {
5 id: root
6 readonly property alias mice: priv.miceCount
7 readonly property alias keyboards: priv.keyboardCount
9 QtObject {
10 id: priv
12 property var mice: new Array()
13 property var keyboards: new Array()
15 property int miceCount: 0
16 property int keyboardCount: 0
18 function addMouse(devicePath) {
19 mice.push(devicePath);
20 miceCount++;
21 }
23 function addKeyboard(devicePath) {
24 keyboards.push(devicePath);
25 keyboardCount++;
26 }
28 function removeDevice(devicePath) {
29 for (var i = 0; i < priv.mice.length; i++) {
30 if (priv.mice[i] == devicePath) {
31 priv.mice.splice(i, 1);
32 priv.miceCount--;
33 }
34 }
35 for (var i = 0; i < priv.keyboards.length; i++) {
36 if (priv.keyboards[i] == devicePath) {
37 priv.keyboards.splice(i, 1);
38 priv.keyboardCount--;
39 }
40 }
41 }
42 }
44 InputDeviceInfo {
45 id: inputInfo
47 onNewDevice: {
48 var device = inputInfo.get(inputInfo.indexOf(devicePath));
49 if (device === null) {
50 return;
51 }
53 var hasMouse = (device.types & InputInfo.Mouse) == InputInfo.Mouse
54 var hasTouchpad = (device.types & InputInfo.TouchPad) == InputInfo.TouchPad
55 var hasKeyboard = (device.types & InputInfo.Keyboard) == InputInfo.Keyboard
57 if (hasMouse || hasTouchpad) {
58 priv.addMouse(devicePath);
59 } else if (hasKeyboard) {
60 // Only accepting keyboards that do not claim to be a mouse too
61 // This will be a bit buggy for real hybrid devices, but doesn't
62 // fall for Microsoft mice that claim to be Keyboards too.
63 priv.addKeyboard(devicePath)
64 }
65 }
66 onDeviceRemoved: {
67 priv.removeDevice(devicePath)
68 }
69 }