Java 无法在scala中使用Leapmotion控制器移动鼠标和捕捉手势
我尝试使用leapmotion jar复制鼠标的手势和手的运动,我创建了两种方法Java 无法在scala中使用Leapmotion控制器移动鼠标和捕捉手势,java,scala,leap-motion,Java,Scala,Leap Motion,我尝试使用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(gestu
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)
}
}
}
def executegstrue(手势:手势)={
val robot=新机器人();
手势匹配{
大小写手势.Type.Type_圆圈=>{
println(“它是圆”)
val circle=新的圆圈测试(手势);
if(circle.pointable().direction().angleTo(circle.normal()){
val swipe=新SWIPPEGESTURE(手势)
如果(滑动方向().getX()>0){
println(“向右滑动”)
}否则{
println(“向左滑动”)
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyrease(KeyEvent.VK_ALT);
robot.keyrease(KeyEvent.VK_F4);
}
}
大小写手势.Type.Type\u屏幕\u点击=>{
val ScreenTap=新屏幕点击手势(手势)
println(“屏幕点击”+屏幕点击.id())
}
case-signature.Type.Type_-KEY_-TAP=>{
val KeyTap=新按键手势(手势)
println(“按键点击”+KeyTap.id())
机器人鼠标按键(InputEvent.BUTTON1_掩码)
robot.mouseRelease(InputEvent.BUTTON1_掩码)
}
case=>println(“其他东西!!……”)
}
}
def执行移动(帧:帧){
val 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().magnet().toInt
val位置=finger.tiposition()
如果(tipVelocity>2){
prevPoint.x=nextPoint.x
prevPoint.y=下一个点.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
如果((diffx>4)和(diffy>4)){
robot.mouseMove(nextPoint.x,nextPoint.y)
//移动鼠标(prevPoint.x,prevPoint.y,nextPoint.x,nextPoint.y,200,30)
}
}
}
Executemovement将鼠标移向您的手的方向,当我注释Executemovement时,executeGesture会识别所有手势,但当我运行这两种方法时,它不会检测到按键和屏幕点击事件。我无法理解其背后的原因。我怀疑您在运行bo时跳过了帧th功能。如果onFrame处理程序返回速度不够快,Leap Motion软件将跳过帧,直到完成为止。屏幕和按键手势仅出现在单个帧中,因此如果代码正在删除帧,则很容易错过它们。解决方法是保存对代码处理的最后一帧的引用,并通过t进入
Frame.signities(sinceFrame)
功能。这将为您提供在sinceFrame和当前帧之间生成的所有手势对象