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