Remote Keypad for PC

Control PC remotely via WiFi

Remote Keypad for PC

This page explains how to remote control a PC via WiFi using an Android phone with RoboRemo app. In this demo I control the arrow keys, but it can be easily extended to control other keys as well.

Materials needed:
- PC with Java Development Kit (JDK) installed, connected to a WiFi router
- Android phone with RoboRemo app installed, connected to the same WiFi router

Introduction

Here is how it works: I start a special java program on PC, which creates a TCP ServerSocket and waits for the RoboRemo to connect. I connect with RoboRemo to that socket. I send commands from RoboRemo and the java program on PC receives those commands and simulates the corresponding key press or key release event. It is that simple.

Steps:

1.
Save this java code to a file called RemoteKeys.java , modify if needed and compile it (javac RemoteKeys.java):

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class RemoteKeys {

    private ServerSocket ser;
    private Socket soc;
    private InputStream is;
    private OutputStream os;
    private BufferedReader br;
    private Robot robot;
    private int screenH, screenW;

    
    
    private void closeServer() {
        if(ser!=null) {
            try {
                ser.close();
            } catch (IOException e) {
                debug(e.toString());
            }
        }
        ser = null;
    }
    
    
    private void closeSoc() {
        if(is!=null) {
            try {
                is.close();            
            } catch(Exception e) {
                debug(e.toString());
            }
        }
        is = null;
        
        if(os!=null) {
            try {
                os.close();            
            } catch(Exception e) {
                debug(e.toString());
            }
        }
        os = null;
        
        if(soc!=null) {
            try {
                soc.close();            
            } catch(Exception e) {
                debug(e.toString());
            }
        }
        soc = null;
    }
    
    
    
    public void start() {
            
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice screen = ge.getDefaultScreenDevice();     
        GraphicsConfiguration screenConfig = screen.getDefaultConfiguration();
        
        screenW = screenConfig.getBounds().width;
        screenH = screenConfig.getBounds().height;
         
   
        try {
            robot = new Robot(screen);
        } catch(Exception e) {
            debug(e.toString());
            return;
        }
        
        
        try {
            ser = new ServerSocket();
            ser.setReuseAddress(true);
            ser.setSoTimeout(5000);
            ser.bind(new InetSocketAddress(9876));
            
            soc = ser.accept();    // expires in 5 seconds
            
        } catch(Exception e) {
            debug(e.toString());
            closeSoc();
            closeServer();
            return;
        }
        
        debug("client accepted");
                
        try {    
            is = soc.getInputStream();
            os = soc.getOutputStream();
        } catch(Exception e) {
            debug(e.toString());
            closeSoc();
            closeServer();
            return;
        }
        
        debug("streams opened ok");
        
        try {
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));    
        } catch(Exception e) {
            debug(e.toString());
            closeSoc();
            closeServer();
            return;
        }
        
        debug("BufferedReader opened ok");
        
        int timeout = 0;
        
        String line = null;
        
        
    
        while(true) {
        
            try {
                line = br.readLine();
            } catch(Exception e) {            
            }
            
            if(line==null) {
                try {
                    Thread.sleep(5);
                } catch(Exception e) {
                    
                }
                timeout++;
                if(timeout>200) break; // if nothing received for 1 second
                
                continue;
            }
            
            
            //     actions from RoboRemo   ---------->    computer keys
        
            if(line.startsWith("u1")) robot.keyPress(KeyEvent.VK_UP);
            if(line.startsWith("d1")) robot.keyPress(KeyEvent.VK_DOWN);
            if(line.startsWith("l1")) robot.keyPress(KeyEvent.VK_LEFT);
            if(line.startsWith("r1")) robot.keyPress(KeyEvent.VK_RIGHT);
            
            if(line.startsWith("u0")) robot.keyRelease(KeyEvent.VK_UP);
            if(line.startsWith("d0")) robot.keyRelease(KeyEvent.VK_DOWN);
            if(line.startsWith("l0")) robot.keyRelease(KeyEvent.VK_LEFT);
            if(line.startsWith("r0")) robot.keyRelease(KeyEvent.VK_RIGHT);
        
        }
        
        
        closeSoc();
        closeServer();
    }
    
    
    
    
    public static void main(String[] args) {
        new RemoteKeys().start();
    }
    
    
    
    static private void debug(String st) {
        System.out.println(st);
    }

}

2.
Download this RoboRemo interface:

remote keypad.interface

Copy the interface file to your Android device, then import in RoboRemo using menu -> interface -> import.

3.
Open RoboRemo app select menu -> connect -> TCP -> other and enter the IP address of your PC and port 9876
(example: 192.168.0.5:9876).
RemoteKeys.java uses port 9876, you can change it if you want.
After entering IP and port, DON'T press OK.

4.
Run the RemoteKeys.class file using the command java RemoteKeys.
It waits 5 seconds for phone to connect, you can change this timeout in the java code.

5.
Press OK on phone.

6.
Now you can control the arrow keys of your PC using the phone, enjoy :)