Results 1 to 5 of 5

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

  1. #1
    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 06:07 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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();
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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);     
         
          }
    
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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);

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. How to use Event class of java?
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 08:15 AM
  3. How to create a class in java script
    By Lawford in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 02:16 AM
  4. How to create mouse event using Robot class in java?
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 08:03 PM
  5. How to create an inner class in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 01-02-2010, 04:40 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,619,860.24268 seconds with 17 queries