Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Simulating mouse click and Keyboard press with Java and Pixel color determiner

In this article we will simulate some Java events like mouse click and keyboard press.

Remember while using this program don’t click or press any keys and also don’t open any of your important programs. If you click on any other window then all the events produced from this program will apply on that window.

Now we will discuss the steps to be followed :

First, we need to import all the necessary classes required by our program. These are:

Listing1: Importing the classes

import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

A small description of the imports:

  1. FlowLayout is used to set the layout of the Elements which we place inside the Frame.
  2. Robot class is for simulating the clicks and also for pixel information.
  3. ActionEvent, InputEvent, KeyEvent are for operation to be performed when a particular event occurs.
  4. ActionListener is used to listen to any event that may occur.
  5. JButton is for defining a button.
  6. JFrame is for defining a frame.
  7. JLabel is for defining a label in the frame.
  8. JTextField is for making a textfield in the frame.

The use of these imports will be clearer when we use them in our program.

Now we define the class and the main function. I took the name of class as Fun but you may change it also.

Listing 2: Declaring classes and the main method

public class Fun extends JFrame{
	JLabel l;
	Robot r;
	JTextField xcord,ycord;
public static void main(String args[])
	{
		new Fun().start();
	
}

Here we have 3 variables :

  1. l for a label information in Jframe
  2. r as Robot class object which will perform all the simulation clicks
  3. xcords and ycords for accepting the values of coordinates which will be used for pixel informaion.
  4. In main method we call a function called start which contains all the processing.

Listing3: Declaring the frame along with frame elements inside start method

void start()
	{
		try
		{
			JFrame f=new JFrame("Fun");
			JTextField txt=new JTextField(20);
			l=new JLabel("Printing msg without touching keyboard.");
			JLabel l2=new JLabel("Pixel color determiner.Enter x and y coordinates");
			xcord=new JTextField(6);
			ycord=new JTextField(6);
			JButton but=new JButton("Determine");
			but.setEnabled(false);
			f.add(txt);
			f.add(l);
			f.add(l2);
			f.add(xcord);
			f.add(ycord);
			f.add(but);
			f.setSize(500,200);
			f.setVisible(true);
			f.setLayout(new FlowLayout());
			f.setDefaultCloseOperation(EXIT_ON_CLOSE);
			f.setLocation(400,400);

Here we have defined :

  1. A new frame called f
  2. txt as a textfield which will contain the key pressed from the program
  3. l for showing the notifications
  4. l2,xcord,ycord,but for pixel information purpose
  5. Now we add the textfield labels and the button to the frame using the add method.
  6. We set the size of frame using the setSize method and make it visible
  7. The layout is set to FlowLayout .
  8. EXIT_ON_CLOSE will ensure that application closes when one clicks on x button
  9. The location of frame is set to 400,400.

Now we will produce the key press from the keyboard:

Listing 4 : Simulating the keyboard keypress event

r=new Robot();
			r.setAutoDelay(900);
			
		r.keyPress(KeyEvent.VK_T);
		r.keyPress(KeyEvent.VK_H);
		r.keyPress(KeyEvent.VK_I);
		r.keyPress(KeyEvent.VK_S);
		r.keyPress(KeyEvent.VK_SPACE);
		r.keyPress(KeyEvent.VK_I);
		r.keyPress(KeyEvent.VK_S);
		r.keyPress(KeyEvent.VK_SPACE);
		r.keyPress(KeyEvent.VK_F);	
		r.keyPress(KeyEvent.VK_U);
		r.keyPress(KeyEvent.VK_N);

Here we define a new object of Robot class which is r and set the Auto Delay to 900 millisecond so that after each keypress there will be a delay of 900 millisecond.

keyPress function is used to make a key press. Here VK_T defines that key T will be produced on the output screen. Actually whatever is written after VK_ is the key which will be pressed.

Now we will move the mouse using program:

Listing 5 : Simulating the mouse movement as well as producing mouse clicks

   l.setText("Moving mouse without touching");
		r.mouseMove(410,110);
		r.mouseMove(410,210);
		r.mouseMove(410,310);
		r.mouseMove(410,410);
		
		l.setText("Performing Mouse right click");
		r.mousePress(InputEvent.BUTTON3_MASK);
		r.mouseRelease(InputEvent.BUTTON3_MASK);
		
		l.setText("Performing Mouse left click after moving mouse to right");
		r.mouseMove(700,410);
		r.mousePress(InputEvent.BUTTON1_MASK);
		r.mouseRelease(InputEvent.BUTTON1_MASK);

Here we use the mouseMove function and give the coordinates where the mouse will move to as arguments. The mousePress will create a mouse click event .After mouse click we need to release it,which is done using the mouseRelease function and InputEvent.BUTTON3_MASK will make a right mouse click .Similarly BUTTON1_MASK is used for left mouse button click.

Now we will code for pixel color information :

Listing6: Pixel information at particular location

	but.setEnabled(true);
		  but.addActionListener(
	    	      new ActionListener(){
	    	        public void actionPerformed(
	    	                        ActionEvent e){
		l.setText(r.getPixelColor(Integer.parseInt(xcord.getText()), Integer.parseInt(ycord.getText())).toString());
	    	        }
	    	      }
	    	    );
		}
		catch(Exception e)
		{
			
		}
	}
}
}

Here we first make an action listener so that when a person click on submit button then it gets executed.

We use the getPixelColor method and pass the x and y coordinates values as argument and this will return the rgb color information on that particular coordinate location.

You can see below the full source code of the application:

import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Fun extends JFrame{
	JLabel l;
	Robot r;
	JTextField xcord,ycord;
	void start()
	{
		try
		{
			JFrame f=new JFrame("Fun");
			JTextField txt=new JTextField(20);
			l=new JLabel("Printing msg without touching keyboard.");
			JLabel l2=new JLabel("Pixel color determiner.Enter x and y coordinates");
			xcord=new JTextField(6);
			ycord=new JTextField(6);
			JButton but=new JButton("Determine");
			but.setEnabled(false);
			f.add(txt);
			f.add(l);
			f.add(l2);
			f.add(xcord);
			f.add(ycord);
			f.add(but);
			f.setSize(500,200);
			f.setVisible(true);
			f.setLayout(new FlowLayout());
			f.setDefaultCloseOperation(EXIT_ON_CLOSE);
			f.setLocation(400,400);
			
			
			r=new Robot();
			r.setAutoDelay(900);
			
		r.keyPress(KeyEvent.VK_T);
		r.keyPress(KeyEvent.VK_H);
		r.keyPress(KeyEvent.VK_I);
		r.keyPress(KeyEvent.VK_S);
		r.keyPress(KeyEvent.VK_SPACE);
		r.keyPress(KeyEvent.VK_I);
		r.keyPress(KeyEvent.VK_S);
		r.keyPress(KeyEvent.VK_SPACE);
		r.keyPress(KeyEvent.VK_F);	
		r.keyPress(KeyEvent.VK_U);
		r.keyPress(KeyEvent.VK_N);
		
	    l.setText("Moving mouse without touching");
		r.mouseMove(410,110);
		r.mouseMove(410,210);
		r.mouseMove(410,310);
		r.mouseMove(410,410);
		
		l.setText("Performing Mouse right click");
		r.mousePress(InputEvent.BUTTON3_MASK);
		r.mouseRelease(InputEvent.BUTTON3_MASK);
		
		l.setText("Performing Mouse left click after moving mouse to right");
		r.mouseMove(700,410);
		r.mousePress(InputEvent.BUTTON1_MASK);
		r.mouseRelease(InputEvent.BUTTON1_MASK);
		
		but.setEnabled(true);
		  but.addActionListener(
	    	      new ActionListener(){
	    	        public void actionPerformed(
	    	                        ActionEvent e){
		l.setText(r.getPixelColor(Integer.parseInt(xcord.getText()), Integer.parseInt(ycord.getText())).toString());
	    	        }
	    	      }
	    	    );
		}
		catch(Exception e)
		{
			
		}
	}
	public static void main(String args[])
	{
		new Fun().start();
	
}}

Output :

img1

I hope you liked the article. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login