the Java.awt.Robot class is used to control the mouse and keyboard. Once you get this kind of control, you can do any type of operation with the mouse and keyboard through your Java code. This class is typically used for automated testing. The following code sample shows you how the robot class handles keyboard events. If you run this code and open Notepad, you will see Hi caoer in Notepad. Try it quickly.
Copy Code code 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 5-second delay so you can open Notepad.
Robot began to write
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 ();
}
}
}
users have perfected the above code:
Copy Code code 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 (). EXEC ("notepad");
For Linux.
Runtime.getruntime (). EXEC ("gedit");
Define a 5-second delay so you can open notepad haha
Robot began to write
Robot.delay (3000);
for (int i = 0; i < 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 ();
}
}
}