Skip to content
benoitvasseur edited this page Sep 5, 2012 · 1 revision

Controlib Sample code

Buttons

This sample code will learn you how to manage buttons on a remote with the controlib project.

I Bean

For using buttons, we have create a specific bean that will be used to send data to the server. This bean corresponds to “CLButtonPressure.java” and is localized in the library “controlib-utility”.

This bean only has one attribute, corresponding to its id on the remote.

II Client

III Server : Observer

This object will observe the socket and will act when finding the right bean instance (here CLButtonPressure).

When one of these beans appears on the socket, this means that the client has used a button and is waiting for an action to be done. Each buttons have an id on the remote, then when you make this observer you have to know which id corresponds to which action. For example here is one of our observer corresponding to a simple mouse remote with only two buttons (right and left click) :

  // Corresponds to the buttons ids
  final static private int MOUSE1 = 0;
  final static private int MOUSE2 = 1;


  @Override
   public void update(Observable o, Object arg) throws AWTException
   {
     if (((CLSerializable) arg).getType().equals("button-pressure"))
     {
        Robot robot = new Robot();
  
        // Get the button id from the bean
       switch (((CLButtonPressure) arg).getButtonId())
       {
         case MOUSE1:
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            break;
         case MOUSE2:
            robot.mousePress(InputEvent.BUTTON2_MASK);
            robot.mouseRelease(InputEvent.BUTTON2_MASK);
            break;
         case default:
            break;
       }
     }
  }

TouchPad

I Bean

For making a Touchpad, we have create a specific bean that will be used to send data to the server. This bean corresponds to “CLVector.java” and is localized in the library “controlib-utility”.

This bean represents a vector, it allows the client to set 2 attributes corresponding to a difference on the x-axis and the y-axis from the last know position.

II Client : How to get position on the pad

III Server : Observer

This object will observe the socket and will act when finding the right bean instance (here CLVector).

Since CLVector corresponds to the last known position, you have to get the current pointer position and then move it using the new values. Here is a code sample used by our plugin to do this :

  @Override
   public void update(Observable o, Object arg) throws AWTException
   {
     if (((CLSerializable) arg).getType().equals("vector"))
     {
        // Get the current mouse pointer position
        int mouseX = MouseInfo.getPointerInfo().getLocation().x;
        int mouseY = MouseInfo.getPointerInfo().getLocation().y;

        // Get the values of the bean sent by the client
        float x = ((CLVector) arg).getX();
        float y = ((CLVector) arg).getY();

        // Move the mouse using the bean values
        Robot robot = new Robot();
        robot.mouseMove(Math.round(mouseX + x), Math.round(mouseY + y));
     }
  }