The Java. awt. Robot class is used to control the mouse and keyboard. Once you get this control, you can perform any type of operation with the mouse and keyboard through your Java code. This class is usually used for automated testing. The following code example shows how the Robot class handles Keyboard Events. If you run this code and start notepad, you will see hi caoer in notepad. Please try it now.
Copy codeThe Code is as follows: import java. awt. AWTException;
Import java. awt. Robot;
Import java. awt. event. KeyEvent;
Public class RobotExp {
Public static void main (String [] args ){
Try {
Robot robot = new Robot ();
// Define a latency of 5 seconds so that you can enable notepad
// Start Robot Writing
Robot. delay (5000 );
Robot. keyPress (KeyEvent. VK_H );
Robot. keyPress (KeyEvent. VK_ I );
Robot. keyPress (KeyEvent. VK_SPACE );
Robot. keyPress (KeyEvent. VK_C );
Robot. keyPress (KeyEvent. VK_A );
Robot. keyPress (KeyEvent. VK_O );
Robot. keyPress (KeyEvent. VK_E );
Robot. keyPress (KeyEvent. VK_R );
} Catch (AWTException e ){
E. printStackTrace ();
}
}
}
The above code has been improved:
Copy codeThe Code is as follows: import java. awt. AWTException;
Import java. awt. Robot;
Import java. awt. event. KeyEvent;
Import java. io. IOException;
Public class RobotExp {
Public static void pressKey (Robot robot, int keyvalue ){
Robot. keyPress (keyvalue );
Robot. keyRelease (keyvalue );
}
Public static void pressKeyWithShift (Robot robot, int keyvalue ){
Robot. keyPress (KeyEvent. VK_SHIFT );
Robot. keyPress (keyvalue );
Robot. keyRelease (keyvalue );
Robot. keyRelease (KeyEvent. VK_SHIFT );
}
Public static void closeApplication (Robot robot ){
// PressKey (robot, KeyEvent. VK_ALT );
// PressKey (robot, KeyEvent. VK_F4 );
Robot. keyPress (KeyEvent. VK_ALT );
Robot. keyPress (KeyEvent. VK_F4 );
Robot. keyRelease (KeyEvent. VK_ALT );
Robot. keyRelease (KeyEvent. VK_F4 );
// For linux.
// Robot. keyPress (KeyEvent. VK_ALT );
// Robot. keyPress (KeyEvent. VK_W );
// Robot. keyRelease (KeyEvent. VK_ALT );
// Robot. keyRelease (KeyEvent. VK_W );
Robot. keyPress (KeyEvent. VK_N );
Robot. keyRelease (KeyEvent. VK_N );
}
Public static void main (String [] args) throws IOException {
Try {
Robot robot = new Robot ();
Runtime.getruntime(cmd.exe c ("notepad ");
// For linux.
// Runtime.getruntime(cmd.exe c ("gedit ");
// Define a latency of 5 seconds so that you can enable notepad haha
// Start Robot Writing
Robot. delay (3000 );
For (int I = 0; I <100; I ++ ){
PressKeyWithShift (robot, KeyEvent. VK_H );
PressKey (robot, KeyEvent. VK_ I );
PressKey (robot, KeyEvent. VK_SPACE );
// PressKeyWithShift (robot, KeyEvent. VK_H );
PressKeyWithShift (robot, KeyEvent. VK_ I );
PressKey (robot, KeyEvent. VK_SPACE );
PressKey (robot, KeyEvent. VK_A );
PressKey (robot, KeyEvent. VK_M );
PressKey (robot, KeyEvent. VK_SPACE );
PressKey (robot, KeyEvent. VK_T );
PressKey (robot, KeyEvent. VK_H );
PressKey (robot, KeyEvent. VK_E );
PressKey (robot, KeyEvent. VK_SPACE );
PressKey (robot, KeyEvent. VK_J );
PressKey (robot, KeyEvent. VK_A );
PressKey (robot, KeyEvent. VK_V );
PressKey (robot, KeyEvent. VK_A );
PressKey (robot, KeyEvent. VK_SPACE );
PressKey (robot, KeyEvent. VK_R );
PressKey (robot, KeyEvent. VK_O );
PressKey (robot, KeyEvent. VK_ B );
PressKey (robot, KeyEvent. VK_O );
PressKey (robot, KeyEvent. VK_T );
// VK_ENTER
PressKey (robot, KeyEvent. VK_ENTER );
// PressKey (robot, KeyEvent .);
}
CloseApplication (robot );
// Robot. keyPress (KeyEvent. VK_SPACE );
} Catch (AWTException e ){
E. printStackTrace ();
}
}
}