The mobile phone controls the computer's mobile phone to simulate mouse movement

  Recently, I made a small demo where a mobile phone controls a computer through wifi. There are some small bugs in the mouse movement control. Here is a record of the correct movement method. The first method is also the easiest to think of, that is, through the memory. Once the mouse position, calculate the distance deviation, the code is as follows:

                int x= (int) event.getX();
                int y= (int) event.getY();
                int distantX=0;
                int distantY=0;
                synchronized (msgInfo){
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            msgInfo.setX(x);
                            msgInfo.setY(y);
                            msgInfo.setEventType(MsgInfo.MOUSE_MOVED);
                            msgInfo.setId(MsgInfo.ID_MOUSE);
                            oldx=x;
                            oldy=y;
                            msgInfo.setX(0);
                            msgInfo.setY(0);
                            //发送按下指令
                            break;
                        case MotionEvent.ACTION_MOVE:
                            msgInfo.setX(x);
                            msgInfo.setY(y);
                            ++flag;
                            if (flag==1){
                                oldx=x;
                                oldy=y;
                            }
                            distantX=x-oldx;
                            distantY=y-oldy;
                            oldx=x;
                            oldy=y;
                            msgInfo.setX(distantX);
                            msgInfo.setY(distantY);
                            //发送移动指令
                            break;
                        case MotionEvent.ACTION_UP:
                            msgInfo.setX(x);
                            msgInfo.setY(y);
                            flag=0;
                            distantX=x-oldx;
                            distantY=y-oldy;
                            oldx=x;
                            oldy=y;
                            msgInfo.setX(distantX);
                            msgInfo.setY(distantY);
                            //发送抬起指令
                            break;
                    }

    But in fact, android provides us with a more convenient method , which is to use GestureDetector , which is the gesture recognition of android. It provides the scroll method to help us calculate the sliding distance, which is very simple to implement. So the second method has a small amount of code, so post the code:


 final  GestureDetector detector=new GestureDetector(this,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                synchronized (msgInfo){
                    msgInfo.setX((-(int)distanceX)*mouseDpi);//转化为相反数,否则方向相反
                    msgInfo.setY((-(int)distanceY)*mouseDpi);
                    msgInfo.setEventType(MsgInfo.MOUSE_MOVED);
                    msgInfo.setId(MsgInfo.ID_MOUSE);
                    Log.i("TAG", "onScroll: "+distanceX+","+distanceY);
                    Log.i("TAG", "e1: "+e1.getPointerCount()+",e2:"+e2.getPointerCount());
                    if (e2.getPointerCount()==2){
                        if (distanceY>0){
                            msgInfo.setEventType(MsgInfo.WHEEL_DOWN_PRESSED);
                        }else {
                            msgInfo.setEventType(MsgInfo.WHEEL_UP_PRESSED);
                        }
                    }

                }
                return true;
            }
        });
detector.onTouchEvent(event);//在监听函数onTouch里添加这句,替换掉原来的监听事件,最后记得返回true
Note: 1. msgInfo is the packaging of the data that needs to be sent through the socket

      2. The judgment statement after method 2 is to judge multi-touch, and then simulate the mouse wheel sliding

Guess you like

Origin blog.csdn.net/Nbin_Newby/article/details/79726856