Search code examples
androidcoordinatesmousescreen-resolution

How do I map my phone's screen coordinates to my pc screen?


I finally developed my app where I can control PC mouse with my Android phone. I am using touchscreen to control the mouse.

The issue is that the mouse cursor moves only within a certain region restricted by the phone's screen size. I want to be able to move the cursor everywhere? Do I need some kind of mapping?

This is how I am sending my coords from the phone:

public boolean onTouchEvent(MotionEvent evt)
{   
    String coords = Math.round(evt.getX()) + ", " + Math.round(evt.getY());

    Log.d(TAG, coords);

    msgIO.sendMessage(soc, coords);

    return true;
}

To clarify: say phone's screen is limited to 300x700 and PC screen is 1080x720. Now if I use my phone's touchscreen to send coords it will only move the mouse cursor on pc side within a 300x700 rectangle. I want to move it within 1080x720 rectangle.


Solution

  • I think you can solve it by Math.

    You need to send 4 parameters to PC.

    String coords = Phone_Touched_X;
    coords += ", "
    coords += Phone_Touched_Y
    coords += ", "
    coords += Phone_Screen_X
    coords += ", "
    coords += Phone_Screen_Y
    

    On PC side:

    Position_X = PC_Screen_X * Phone_Touched_X / Phone_Screen_X;
    Position_Y = PC_Screen_Y * Phone_Touched_Y / Phone_Screen_Y;
    

    Example:

    You touched 200,200 on a 300x700 phone screen. And send it to a 1080x720 PC.

    Position_X = 1080 * 200 / 300 = 720
    Position_Y = 720 * 200 / 700 = 205
    

    Note that, you need to consider also if you are operating the phone in portrait mode. In that case, you should pass 700x300 instead of 300x700.

    Position_X = 1080 * 200 / 700 = 308
    Position_Y = 720 * 200 / 300 = 480