Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to create key press event using Robot class in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 72
How to create key press event using Robot class in java?
  

Hi friends,
I am last year Computer Science student. As part of my syllabus I am working on one project. In that project I have to create key press event using Robot class. I tried various method. But none of them worked out. Can anyone tell me how to create key press event using Robot class in java? Please help me.
Thank you.


Last edited by Kasper : 03-02-2010 at 05:07 PM.
Reply With Quote
  #2  
Old 03-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
Re: How to create key press event using Robot class in java?

In following program I have use the java.awt.Robot class for generating a key press event. In following program I have use keyPress(int keyCode) method to produce this event. It is very easy process. Just try to understand it. In following program I have use keypressEG class to create key press event.


Code:
package sample.kode.jav.awt;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class keypressEG {
    public static void main(String[] args) {
        try {
            Robot robots1 = new Robot();

           
            robots1.delay(3560);
                                     
            
            robots1.keyPress(KeyEvent.VK_Q);
            robots1.keyPress(KeyEvent.VK_W);
            robots1.keyPress(KeyEvent.VK_E);
            robots1.keyPress(KeyEvent.VK_R);
            robots1.keyPress(KeyEvent.VK_T);
            robots1.keyPress(KeyEvent.VK_Y);
        } catch (AWTException es) {
            es.printStackTrace();
        }
    }
}
Reply With Quote
  #3  
Old 03-02-2010
Member
 
Join Date: May 2008
Posts: 2,007
Re: How to create key press event using Robot class in java?

Hey it is very easy process to create key press event using Robot class in java. You have to just create one class and using that class you have to create one Robot class. Now create object of that Robot class and using that robot object create key press event.


Code:
 
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class KeypressExample {
  public static void main(String[] args) throws Exception {
    Robot robo = new Robot();

    robo.delay(3680);

    robo.keyPress(KeyEvent.VK_Q);
    robo.keyPress(KeyEvent.VK_W);
    robo.keyPress(KeyEvent.VK_E);
    robo.keyPress(KeyEvent.VK_R);
    robo.keyPress(KeyEvent.VK_T);
    robo.keyPress(KeyEvent.VK_Y);
  }
}
Reply With Quote
  #4  
Old 03-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
Re: How to create key press event using Robot class in java?

Hey ther is no need to use robot class you can do it without using key event. You have to use artificial key event to do this. Just use artificial key event to generate a "simple caracter" key press (for instance KeyEvents.VK_A). I have written following program for you. Just try to understand it. In following program I use KeypressEG class to create key press event.

Code:
import java.awt.*;
import java.awt.event.*;

public class 
KeypressEG {

      public static void main(String[] args) throws AWTException {
     
            
            Frame fsw = new Frame();
           
            fsw.addKeyListener(new KeyAdaptewr() {      public void
keyPressed(KeyEvent evsw) {System.out.println
                                               
      (Character.isUpperCase( ev.getKeyChar()) ?       "Caps Lock
ON" :"go to home");}           
            });
            
            fsw.show();
            
            Robot roboswt = new Robot();
            
            roboswt.keyPress(KeyEvent.VK_A);     
     
      }

}
Reply With Quote
  #5  
Old 03-02-2010
Member
 
Join Date: May 2008
Posts: 2,291
Re: How to create key press event using Robot class in java?

The Robot class is used to temporarily take control of the mouse and keyboard input functionality at the operating-system level. Robot class is one of the most common classes used by java programmer for doing automated testing. It is very easy to use Robot class. You can declare Robot class in following way.

Robot robo = new Robot();

You can create keypress event in following ways.

Code:
robo.keyPress(KeyEvent.VK_D);
robo.keyRelease(KeyEvent.VK_D);

robo.keyPress(KeyEvent.VK_E);
robo.keyRelease(KeyEvent.VK_E);

robo.keyPress(KeyEvent.VK_M);
robo.keyRelease(KeyEvent.VK_M);

robo.keyPress(KeyEvent.VK_O);
robo.keyRelease(KeyEvent.VK_O);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create key press event using Robot class in java?"
Thread Thread Starter Forum Replies Last Post
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
How to use Event class of java? Protectors Software Development 5 25-02-2010 07:15 AM
How to create a class in java script Lawford Software Development 5 11-02-2010 01:16 AM
How to create mouse event using Robot class in java? Luis-Fernando Software Development 4 03-02-2010 07:03 PM
How to create an inner class in java? MABON Software Development 4 01-02-2010 03:40 PM


All times are GMT +5.5. The time now is 09:42 AM.