In-depth explanation of how to control the mouse and keyboard using Java Robot

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.