Wednesday, December 7, 2016

Global Tracking and Controlling your mouse using Java

This post will help you track your mouse movements. It will also help you to re-position your mouse or make mouse clicks through program..
This can be used as a base for creating game modules where you would like to control your mouse or restrict your mouse movements

Features of program:
1) Program opens a small frame.
2) Program makes the mouse click on frame and drag it
2) On moving mouse inside frame, mouse coordinate will be shown.
3) This program will also track movement of mouse even it is outside frame window

Reference:
http://stackoverflow.com/questions/18321877/tracking-mouse-movement-in-java
http://omtlab.com/java-control-the-mouse-pointer-and-click/

Language Used:
Java

Git Location:
https://github.com/csanuragjain/extra/tree/master/MouseControllerTracker

Program:

Variable declaration:
      JLabel coordinate=null;  
      final static int frameWidth=500;  
      final static int frameHeight=500;  
      static int frameStartPosX=0;  
      static int frameStartPosY=0;  
      static int frameEndPosX=0;  
      static int frameEndPosY=0;  
      static int mouseRecenterPosX=0;  
      static int mouseRecenterPosY=0;  

How it works:
1) coordinate is the JLabel shown in JFrame
2) frameWidth is the width of the JFrame and frameHeight is height of JFrame
3) frameStartPosX and frameStartPosY will show the starting coordinate of JFrame
4) frameEndPosX and frameEndPosY will show the ending coordinate of JFrame
5) mouseRecenterPosX and mouseRecenterPosY is an arbitrary position where we will drag the frame.

main method:
      public static void main(String[] args) {  
           try {  
           Robot r=new Robot();  
           MouseControllerTracker mt=new MouseControllerTracker();  
           JFrame f=mt.showFrame(frameWidth,frameHeight);  
           mt.trackFrame(f);  
           mt.moveFrameUsingMouse(r);  
           while(true)  
           {  
                mt.trackMouse(r);  
           }  
           } catch (AWTException e) {  
                System.out.println("There was an issue while instantiating Robot class "+e.getMessage());  
           }  
      }  

How it works:
1) We make an object of Robot class which will be used by the helper functions
2) We call the showFrame function which will create a new JFrame with the passed width and height value. It will also return the instance of JFrame created.
3) We call the trackFrame method which tracks the current frame position and update the global variables.
4) We call moveFrameUsingMouse which makes mouse to drag the frame and bring it to certain position
5) We call trackMouse which keeps a track of mouse movement and update in GUI

showFrame method:
      public JFrame showFrame(int frameWidth, int frameHeight)  
      {  
           JFrame f=new JFrame();  
           coordinate=new JLabel();  
           f.add(coordinate);  
           f.setSize(frameWidth, frameHeight);  
           f.setLayout(new FlowLayout(0));  
           f.setVisible(true);  
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
           return f;  
      }  

How it works:
1) We create a Jframe object and add the JLabel.
2) The size of frame is set from the argument values

trackFrame method:
      public void trackFrame(JFrame f)  
      {  
           Point p=f.getLocationOnScreen();  
           frameStartPosX=p.x;  
           frameStartPosY=p.y;  
           frameEndPosX=frameStartPosX+frameWidth;  
           frameEndPosY=frameStartPosY+frameHeight;  
           mouseRecenterPosX=(frameStartPosX+frameEndPosX)/2;  
           mouseRecenterPosY=(frameStartPosY+frameEndPosY)/2;  
      }  

How it works:
1) getLocationOnScreen is used to determine the current start frame coordinates
2) We copy the coordinate in frameStartPosX & frameStartPosY
3) Since we have already defined the frameWidth and frameHeight, so we use the same to get the frame end position and update frameEndPosX and frameEndPosY
4) We update mouseRecenterPosX and mouseRecenterPosY to be at center position.

moveFrameUsingMouse method:
      public void moveFrameUsingMouse(Robot robot)  
      {  
           robot.mouseMove(mouseRecenterPosX, frameStartPosY+10);  
           robot.mousePress(InputEvent.BUTTON1_MASK);  
           for(int i=0;i<200;i++){  
                robot.mouseMove(mouseRecenterPosX+i, mouseRecenterPosY+i);  
                robot.delay(10);  
           }  
           robot.mouseRelease(InputEvent.BUTTON1_MASK);  
      }  

How it works:
1) mouseMove method is used to move the mouse. It takes x and y coordinate as the argument. So firstly we move mouse over frame title bar
2) mousePress is used to make a mouseClick. Here mouse Button1 would be click on frame
3) Now in a loop we make the mouse move to different coordinate so that it makes a feeling of mouse dragging the frame. We introduced a delay so that drag is visible and not happen instantly
4) Finally after drag is done we release the mouse.

trackMouse method:
      public void trackMouse(Robot r)  
      {  
           PointerInfo inf = MouseInfo.getPointerInfo();  
           Point p = inf.getLocation();  
           coordinate.setText("Mouse moved to ("+p.x+","+p.y+")");  
      }  

How it works:
1) We use getPonterInfo method to extract the information about mouse pointer
2) We call getLocation to get current mouse coordinate
3) We update the JLabel on frame to the current coordinate of mouse.

Full Program:
 package com.cooltrickshome;  
 import java.awt.AWTException;  
 import java.awt.FlowLayout;  
 import java.awt.MouseInfo;  
 import java.awt.Point;  
 import java.awt.PointerInfo;  
 import java.awt.Robot;  
 import java.awt.event.InputEvent;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 public class MouseControllerTracker{  
      JLabel coordinate=null;  
      final static int frameWidth=500;  
      final static int frameHeight=500;  
      static int frameStartPosX=0;  
      static int frameStartPosY=0;  
      static int frameEndPosX=0;  
      static int frameEndPosY=0;  
      static int mouseRecenterPosX=0;  
      static int mouseRecenterPosY=0;  
      public static void main(String[] args) {  
           try {  
           Robot r=new Robot();  
           MouseControllerTracker mt=new MouseControllerTracker();  
           JFrame f=mt.showFrame(frameWidth,frameHeight);  
           mt.trackFrame(f);  
           mt.moveFrameUsingMouse(r);  
           while(true)  
           {  
                mt.trackMouse(r);  
           }  
           } catch (AWTException e) {  
                System.out.println("There was an issue while instantiating Robot class "+e.getMessage());  
           }  
      }  
      public JFrame showFrame(int frameWidth, int frameHeight)  
      {  
           JFrame f=new JFrame();  
           coordinate=new JLabel();  
           f.add(coordinate);  
           f.setSize(frameWidth, frameHeight);  
           f.setLayout(new FlowLayout(0));  
           f.setVisible(true);  
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
           return f;  
      }  
      public void trackFrame(JFrame f)  
      {  
           Point p=f.getLocationOnScreen();  
           frameStartPosX=p.x;  
           frameStartPosY=p.y;  
           frameEndPosX=frameStartPosX+frameWidth;  
           frameEndPosY=frameStartPosY+frameHeight;  
           mouseRecenterPosX=(frameStartPosX+frameEndPosX)/2;  
           mouseRecenterPosY=(frameStartPosY+frameEndPosY)/2;  
      }  
      public void moveFrameUsingMouse(Robot robot)  
      {  
           robot.mouseMove(mouseRecenterPosX, frameStartPosY+10);  
           robot.mousePress(InputEvent.BUTTON1_MASK);  
           for(int i=0;i<200;i++){  
                robot.mouseMove(mouseRecenterPosX+i, mouseRecenterPosY+i);  
                robot.delay(10);  
           }  
           robot.mouseRelease(InputEvent.BUTTON1_MASK);  
      }  
      public void trackMouse(Robot r)  
      {  
           PointerInfo inf = MouseInfo.getPointerInfo();  
           Point p = inf.getLocation();  
           coordinate.setText("Mouse moved to ("+p.x+","+p.y+")");  
      }  
 }  

Hope it helps :)

1 comment: