如何分别在同一设备上处理两只鼠标?

时间:2019-08-14 07:32:51

标签: qt qml qt5

我有Qt5.10,可以为Raspberry Pi3交叉编译

我有一个基于QML的程序,可以捕获鼠标滚轮事件并执行一些功能!

我想将另一只鼠标连接到我的Raspberry Pi。因此,每个鼠标在我的程序中将具有不同的功能!

如何区分这两个鼠标设备?

例如,我可以获取每个鼠标ID并采取相应的措施吗?

1 个答案:

答案 0 :(得分:1)

使用MultiPointTouchArea,您可以将2只鼠标作为单独的touchPoints处理。

但是,您将只能采取行动。但是使用pressedreleasedtouchUpdated信号,您可以轻松处理点击/拖动事件:

MultiPointTouchArea {

    mouseEnabled: true

    touchPoints: [
        // your 2 recognizable touchPoints for your 2 mice
        TouchPoint { id: point1 },
        TouchPoint { id: point2 }
    ]

    onPressed: {
        touchPoints.forEach(function(touchPoint) {
            if (touchPoint === point1) {
                console.log("FIRST MOUSE PRESSED:", touchPoint.x, touchPoint.y)
            } else if (touchPoint === point2){
                console.log("SECOND MOUSE PRESSED:", touchPoint.x, touchPoint.y)
            }
        })
    }

    onReleased: {
        touchPoints.forEach(function(touchPoint) {
            if (touchPoint === point1) {
                console.log("FIRST MOUSE RELEASED:", touchPoint.x, touchPoint.y)
            } else if (touchPoint === point2){
                console.log("SECOND MOUSE RELEASED:", touchPoint.x, touchPoint.y)
            }
        })
    }

    onTouchUpdated: {
        touchPoints.forEach(function(touchPoint) {
            if (touchPoint === point1) {
                console.log("FIRST MOUSE UPDATED:", touchPoint.x, touchPoint.y)
            } else if (touchPoint === point2){
                console.log("SECOND MOUSE UPDATED:", touchPoint.x, touchPoint.y)
            }
        })
    }
}

输出(仅使用单个鼠标进行测试,但应使用2个鼠标):

qml: FIRST MOUSE PRESSED: 418 326
qml: FIRST MOUSE UPDATED: 419 327
qml: FIRST MOUSE UPDATED: 420 327
qml: FIRST MOUSE UPDATED: 421 327
qml: FIRST MOUSE UPDATED: 422 328
qml: FIRST MOUSE UPDATED: 423 328
qml: FIRST MOUSE UPDATED: 424 329
qml: FIRST MOUSE UPDATED: 425 329
qml: FIRST MOUSE UPDATED: 426 329
qml: FIRST MOUSE UPDATED: 427 329
qml: FIRST MOUSE RELEASED: 427 329

不幸的是,我想不出另一个能够捕获多个悬停/车轮事件的解决方案。