How to create mouse event using Robot class in java?
Hello friends,
I am new to this forum. I am working on live project where I am use java as front end and SQL as back end. I want to create mouse event using Robot class. I tried various method but none of them worked out. Can anyone tell me how to create mouse event using Robot class in java? Please give me any example of it.
Thanks in advanced.
Re: How to create mouse event using Robot class in java?
Hey it is very easy process to create mouse event using Robot class in java. You have to just pass Mouseevent() method to robot class to achieve this. In the following program I have automated the process of creating mouse event using the java.awt.Robot class. Just try to understand it.
Code:
package sam.java.example.awt;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MovingMouseEg {
public static void main(String[] args) {
try {
Robot robotic = new Robot();
robot.mouseMove(250, 250);
robotic.mousePress(InputEvent.Button_MASK);
robotic.mouseRelease(InputEvent.Button1_MASK);
robotic.mouseWheel(-150);
} catch (AWTException es) {
es.printStackTrace();
}
}
}
Re: How to create mouse event using Robot class in java?
You have to use two class java.awt.Graphics and java.awt.Graphics2D to do this. It is used to load mouse event in your program. In following program I have create mouseCickEg class to do this. I also have use two variable to decide assign height and width for creating mouse event. I have use getContentPane() to add all these information into one panel.
Code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class mouseCickEg {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame fsw = new JFrame("Test");
fsw.setAlwaysOnTop(true);
Component csw = new JPanel() {
@Override
public void paintComponent(Graphics gws) {
Graphics2D g2ws = (Graphics2D)gws.create();
g2.setColor(Color.graysw);
int wsw = getWidth();
int hsw = getHeight();
g2sw.fillRect(0, 0, wsw,hsw);
g2ws.setComposite(AlphaComposite.Clear);
g2ws.fillRect(w/4, h/4, w-2*(w/4), h-2*(h/4));
}
};
c.setPreferredSize(new Dimension(350, 350));
fsw.getContentPane().add(csw);
fsw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fsw.pack();
fsw.setVisible(true);
com.sun.awt.AWTUtilities.setWindowOpaque(fsw,false);
}
}
Re: How to create mouse event using Robot class in java?
Robot class is used to develop remote system input events for the purposes of testing automation. It is also used to self-running demos. For creating mouse event you have to use mouseMove() method. You can do this in following way.
public void mouseMove(int a, int b)
Where x is referred to X position and
y is referred to Y position
Re: How to create mouse event using Robot class in java?
It is very easy process to create mouse event using Robot class in java. I have written following program for you. Just try to understand it. It is very simple program. In following program I have use MouseEg to define robot class. After creating robot class I have created object of it which is known as "roboq". Go through each line of following program.
Code:
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class MouseEg {
public static void main(String[] args) throws Exception {
Robot roboq = new Robot();
robot.delay(3360);
roboq.keyPress(KeyEvents.VK_Q);
roboq.keyPress(KeyEvents.VK_W);
roboq.keyPress(KeyEvents.VK_E);
roboq.keyPress(KeyEvents.VK_R);
roboq.keyPress(KeyEvents.VK_T);
roboq.keyPress(KeyEvents.VK_Y);
}
}