Skip to main content

Phone as Trackpad for your Computer

I am pretty sure that I am not the only one who crave for a mouse when I don't have one and crave for a trackpad when I don't have access to one. I have a few ideas on turning your phone into a mouse and a trackpad. Let us focus on the later in this blog post. There are two components to this experiment; the first is a server on the computer that you wish to control and the second is a web app that is opened up on your phone that will transmit coordinate data to the server. I think we'll create the server in Python and the web app in JavaScript.

Let's get started with creating the server. We'll use 'Flask' to handle API requests and 'PyAutoGUI' to control the mouse. We'll have APIs defined equivalent to cursor move and click for now.
import pyautogui
from flask import Flask, render_template
app = Flask(__name__)

# Apparently flask doesn't support negative numbers. So offsetting it.
coordinateShifter = 1000

@app.route('/')
def index():
f = open("index.html", "r")
return f.read()

@app.route('/move/<int:x>/<int:y>')
def move(x, y):
pyautogui.move(x - coordinateShifter, y - coordinateShifter)
return 'moving'

@app.route('/click')
def click():
pyautogui.click()
return 'clicking'

To run the python server, simply run the following commands on your terminal.
export FLASK_APP=server.py
flask run -h <your_ip_address>
You can experiment with the move and click APIs by opening the following links on your browser and observe the mouse moving and clicking.
http://<your_ip_address>:5000/move/200/100
http://<your_ip_address>:5000/click
Now, we need to create a Web App that can detect click and move using a touch screen and invoke the above URLs as API requests.

The HTML and CSS may designed to display a DIV that covers the entire screen. The following snippets respectively depict the same.
<div id="fullscreen"></div>

 

#fullscreen {
    background-color: #cccccc;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}


The JavaScript logic would be as follows:

 // Apparently flask doesn't support negative numbers. So offsetting it.

const coordinateShifter = 1000;


const elem = document.getElementById("fullscreen");

var prevX, prevY;


elem.addEventListener('mousedown', function (e) {

    console.log('click');

    var url = window.location.href + 'click';

    var xmlHttp = new XMLHttpRequest();

    xmlHttp.open("GET", url, false);

    xmlHttp.send(null);

}, false);


elem.addEventListener('touchstart', function (e) {

    e = e.originalEvent || e;

    e.stopPropagation();

}, false);


elem.addEventListener('touchmove', function (e) {

    e.preventDefault();

    if (e.targetTouches.length === 1) {

        console.log('move');

        if (typeof prevX === 'undefined') {

            prevX = e.targetTouches[0].pageX;

            prevY = e.targetTouches[0].pageY;

        } else {

            var diffX = e.targetTouches[0].pageX - prevX;

            var diffY = e.targetTouches[0].pageY - prevY;

            prevX = e.targetTouches[0].pageX;

            prevY = e.targetTouches[0].pageY;


            var url = window.location.href + 'move/' + parseInt(coordinateShifter + diffX) + '/' + parseInt(coordinateShifter + diffY);

            var xmlHttp = new XMLHttpRequest();

            xmlHttp.open("GET", url, false);

            xmlHttp.send(null);

        }

    }

    e.stopPropagation();

}, false);


elem.addEventListener('touchend', function (e) {

    console.log('move-done');

    prevX = undefined;

    prevY = undefined;

}, false);


Open up the web app on your phone using the URL http://<your_ip_address>:5000 and start controlling your mouse. You can observe that the controls are not very smooth, but neverthless, it works. The following is a video recording wherein I try to control the mouse using my phone. I did not have another camera to take the entire video, therefore I am just sharing the screencast. Note that only cursor movement and clicking have been implemented. You could also attempt to create a click and drag mechanism or multitouch scrolling as well.

Let me also attach the screenshot of the web app. Plain and simple, huh? If you are worried about the battery consumption, feel free to change the colout to black.

I hope this was inspiring. Feel free to modify the code and create something awesome.

Comments

Popular posts from this blog

How to Create your own Ad Blocker Chrome Extension

Not very long ago, I read an article stating that Google banned advertisement blockers from its stores. There are several websites which does not let you in if you have an Ad blocker extension installed on your browser. I have not investigated on how these websites detect that, nor do I know whether our own version of advertisement blocker would be immune to that. But, isn't it fun to build something like this on our own? As the first step, I found a random website that has a Google Ad. I right clicked on the Google Ad and clicked on Inspect button. As I moved up the Elements inspector, I found that the id of the advertisement starts with "google_ads_". Therefore, I opened the console and typed in the following command: div = document.querySelector('[id^="google_ads_"]') This selected the div  that contained the advertisement. I went ahead and typed in the following command that would remove the div from the page. div.parentElement.remove...

Turning your phone into a virtual-joystick

Update: I kept working on this and I have released it as a package for Windows, Linux and macOS. Check it out: https://github.com/zenineasa/joystick/releases/tag/v1.0.0 -- During the days when I was pursying my master's programme, my friends and I used to occasionally go to a classroom in the university, turn on a projector, connect devices like Nintento Switch or Gaming computers and loads of joysticks, and play different simple multiplayer games; MarioKart was my favourite. From time-to-time, when I get together with people, I ponder that it would be a good idea if I bought such devices. Indeed, I do have a laptop, which could easily run such games; SuperTuxKart is similar enough to MarioKart and it can run on Linux, Windows and Mac. However, I do not have joysticks with me at the moment. Therefore, I think it would be a good idea if I simply worked on a project that would enable using our phones as joysticks. From a high-level, the plan is to host APIs on a NodeJS server that wo...

How to remove Blogger (or any free blogging platform) credits from the footer

I am not sure if removing the footer credits is legal or not, but I just wanted to try doing it. As I searched the internet, I found several pages describing how it could be done by making some adjustments in CSS. However, I could not find an article which uses simple JavaScript commands to do the same. So, I decided the write this. As you can see now, my footer does have Powered By Blogger credit signature. I am intending on keeping it that way until I am sure that removing it would not get me into any legal issues. I am just writing this article for the educational purposes. Right click on the Powered By Blogger  button and click on Inspect to have Chrome Developer tools loaded with the div that holds the credit content. You can see that the element should have a content similar to the following. Notice that the div is having a class value blogger . We can use this to our advantage. <div class="blogger"> <a href="https://www.blogger.com...