Tuesday, 11 March 2014

Small JAVA program to operate mouse from keyboard.

You can use all the functionality provided by mouse through keyboard.
I love JAVA and one of the reason is because it makes some complex tasks look very easy. I am going to use something called ROBOT class in java which is part of java.awt package.

Prerequisite knowledge:

  • Very basic java, if you have made 3-4 simple JAVA program than you should be fine.
    To see about ROBOT class's official documentation see here .
  • Should have java installed, I use eclipse IDE and I recommend it (It's free and opensource and really really awesome).

Difficulty of this tutorial:

Normal.

Lets get started:

First of all create a new java project and make the class which will have the function main() in it.
I named my class KeyboardAsMouse.java.
In the main function make a new instance of class robot class as:
Robot robot = new Robot();
Robot class has a function mouseMove(x,y), This function moves the pointer to (x,y) coordinate of the screen.

Taking Input:

I will use the arrow keys to take input and will use mouseMove(x,y) to move the mouse to x,y.
So, Input keys:
"Arrow keys"         To move the mouse
       "Space key"        For left click                 
"Control Key"         For right click

I am using a simple swing form to get input from keyboard, This can be done by using a KeyListener Like shown below:

// A java frame
JFrame guiFrame = new JFrame();
//Key listener on that jform
guiFrame.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
view raw one hosted with ❤ by GitHub

Moving mouse:

To get current mouse position use following code:

Now all we have to do is move mouse, as I told earlier this can be done by robot.mouseMove() function. A case is shown below for up arrow:

PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
x = (int) b.getX();
y = (int) b.getY();
view raw two hosted with ❤ by GitHub

Mouse clicks:

For mouse click use function robot.mousePress(KeyCode). But, here is a catch; see mousePress() but it will keep on pressing, so you have to apply mouseRelease() just after mousePress().

case KeyEvent.VK_SPACE:
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
break;
view raw three hosted with ❤ by GitHub

Putting this all together:

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class KeyboardAsMouse {
Robot robot;
int x = 50, y = 50;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new KeyboardAsMouse();
}
});
}
public KeyboardAsMouse() {
JFrame guiFrame = new JFrame();
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
x = (int) b.getX();
y = (int) b.getY();
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Mouse");
guiFrame.setSize(50, 50);
guiFrame.setLocationRelativeTo(null);
guiFrame.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
x = (int) b.getX();
y = (int) b.getY();
switch (key) {
case KeyEvent.VK_UP:
robot.mouseMove(x, y - 10);
y -= 10;
break;
case KeyEvent.VK_DOWN:
robot.mouseMove(x, y + 10);
y += 10;
break;
case KeyEvent.VK_LEFT:
robot.mouseMove(x - 10, y);
x -= 10;
break;
case KeyEvent.VK_RIGHT:
robot.mouseMove(x + 10, y);
x += 10;
break;
case KeyEvent.VK_SPACE:
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
break;
case KeyEvent.VK_CONTROL:
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
guiFrame.setAlwaysOnTop(true);
guiFrame.setLocationByPlatform(true);
guiFrame.setVisible(true);
}
}
view raw completeCode hosted with ❤ by GitHub

Thank You Guys, in case of any query or advise comment, feel free to mail me.
Fork this repository on Github

No comments:

Post a Comment