Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Program to demonstrate how to create Keyboard's key press event using the Robot class in Java


Java API provides us with a very powerful class called as Robot class. The Robot class is present in the package java.awt.Robot. It's like a Robot, performing tasks such as key inputs , mouse inputs , screen capture etc ... on your behalf. It was included by Sun in Java API for just testing of swing components. Today, programmers have used it as basic API and created their own powerful API for performing tasks automatically.


Program to demonstrate how to create Keyboard's key press event using the Robot class in Java

package com.hubberspot.robot;

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

public class RobotKeyboardEvent {
 
   public static void main(String[] args) {
  
      try {
   
        Robot robot = new Robot();
    
        robot.delay(1000);
   
 robot.keyPress(KeyEvent.VK_H);
  
        robot.delay(1000);

 robot.keyPress(KeyEvent.VK_E);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_L);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_L);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_O);

 robot.delay(1000);

        robot.keyPress(KeyEvent.VK_SPACE);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_W);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_O);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_R);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_L);

 robot.delay(1000);

 robot.keyPress(KeyEvent.VK_D);
    
     
      } catch (AWTException e) {
     
     e.printStackTrace();
 }
  
   }
  
}  




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot