我正在尝试使用leapmotion jar用手势和手部动作复制鼠标动作,我创建了两种方法
def executeGesture(gesture: Gesture) = {
val robot = new Robot();
gesture.match {
case Gesture.Type.TYPE_CIRCLE => {
println("CIRCLE IT IS")
val circle = new CircleGesture(gesture);
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI / 4) { // Clockwise if angle is less than 90 degrees
// robot.mousePress(InputEvent.BUTTON1_MASK)
// robot.mouseRelease(InputEvent.BUTTON1_MASK)
// robot.mousePress(InputEvent.BUTTON1_MASK)
// robot.mouseRelease(InputEvent.BUTTON1_MASK)
} else {
}
}
case Gesture.Type.TYPE_SWIPE => {
val swipe = new SwipeGesture(gesture)
if (swipe.direction().getX() > 0) {
println("SWIPE Right")
} else {
println("SWIPE Left ")
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);
}
}
case Gesture.Type.TYPE_SCREEN_TAP => {
val ScreenTap = new ScreenTapGesture(gesture)
println("Screen Tap " + ScreenTap.id())
}
case Gesture.Type.TYPE_KEY_TAP => {
val KeyTap = new KeyTapGesture(gesture)
println("Key Tap " + KeyTap.id())
robot.mousePress(InputEvent.BUTTON1_MASK)
robot.mouseRelease(InputEvent.BUTTON1_MASK)
}
case _ => println("Something ELSE!!. .")
}
}
def executeMovement(frame: Frame) {
val robot = new Robot
val finger = frame.fingers().get(0)
val gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
val Xwidth = gd.getDisplayMode().getWidth()
val Xheight = gd.getDisplayMode().getHeight()
val tipVelocity = finger.tipVelocity().magnitude().toInt
val position = finger.tipPosition()
if (tipVelocity > 2) {
prevPoint.x = nextPoint.x
prevPoint.y = nextPoint.y
val mouseX = (Xwidth + Math.round(position.getX() * (Xwidth / 100)))
val mouseY = ((Xheight - (0.0F + position.getY() * 4.0F - Xheight / 5))).toInt
nextPoint.x = mouseX
nextPoint.y = mouseY
val diffx = (prevPoint.x - nextPoint.x).abs
val diffy = (prevPoint.y - nextPoint.y).abs
if ((diffx > 4) & (diffy > 4)) {
robot.mouseMove(nextPoint.x, nextPoint.y)
// moveMouse(prevPoint.x, prevPoint.y, nextPoint.x, nextPoint.y, 200, 30)
}
}
}
Executemovement将鼠标移向你的方向,当我注释掉executetemovement时,executeGesture会识别所有的手势,但是当我运行这两种方法时,它不会检测到Key_tap和Screen_tap事件。 。我无法理解背后的原因
答案 0 :(得分:0)
我怀疑你在运行这两个功能时都在跳帧。如果你的onFrame处理程序没有足够快地返回,Leap Motion软件将跳过帧,直到完成为止。屏幕和KeyTap手势只出现在一个帧中,因此如果您的代码丢帧,很容易错过它们。解决这个问题的方法是保存对代码处理的最后一帧的引用,并将其传递给Frame.gestures(sinceFrame)
函数。这将为您提供在sinceFrame和当前帧之间生成的所有手势对象。