导包
1 2 3 4 5 6 7 8 9 10 | import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.event.InputEvent; import com.sun.glass.events.KeyEvent; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef; import com.sun.jna.platform.win32.WinDef.HWND; |
1、初始化控件
1 | private static Robot robot; |
1 2 3 4 5 6 7 | try { //初始化 控件 robot = new Robot(); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } |
2、检测指定窗口是否在运行,并切换至前排
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // 切换 指定程序窗口 平面窗口标刻 public static boolean switchFrontPlaneWindow(){ // 第一个参数是Windows窗体的窗体类,第二个参数是窗体的标题。 HWND hwnd = User32.INSTANCE.FindWindow(null, "微信"); if (hwnd == null) { System.out.println("微信 未运行"); JOptionPane.showMessageDialog(mHotName, "微信 未运行,请开启!", "错误", JOptionPane.INFORMATION_MESSAGE); return false; } else { System.out.println("微信 正在运行"); // SW_RESTORE User32.INSTANCE.ShowWindow(hwnd, 9); // 放在前面 User32.INSTANCE.SetForegroundWindow(hwnd); //获取现在前台窗口 User32.INSTANCE.GetForegroundWindow(); WinDef.RECT qqwin_rect = new WinDef.RECT(); User32.INSTANCE.GetWindowRect(hwnd, qqwin_rect); int qqwin_width = qqwin_rect.right - qqwin_rect.left; int qqwin_height = qqwin_rect.bottom - qqwin_rect.top; //移动窗口 User32.INSTANCE.MoveWindow(hwnd, 0, 0, qqwin_width,qqwin_height, true); } return true; } |
3、判断窗口是否运行,并模拟按下快捷键运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /*平面标刻*/ public static void GraphicPrint(){ try { // 切换前排指定程序窗口 boolean planeWindow = switchFrontPlaneWindow(); if (planeWindow) { //打印标签 Printer.Tsc243EPrinter( qrCode,mSimNum); Thread.sleep(2000); // F2 执行键 inputF2(); // 向编辑框输入 二维码内容 inputStr(qrCode); } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } |
4、F2
1 2 3 4 5 6 7 | //字符串 F2 程序执行键 public static void inputF2(){ // 这里是按下和释放回车键 robot.keyPress(KeyEvent.VK_F2); robot.keyRelease(KeyEvent.VK_F2); System.out.println("F2"); } |
5、模拟键盘 粘贴内容至可编辑窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 自动录入字符串组合键 Ctrl + V public static void inputStr(String str){ Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(str), null); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); // 回车键 确定 inputEnter(); SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); Date date = new Date(System.currentTimeMillis()); System.out.println("标刻的时间戳="+formatter.format(date)); } |
6、回车键 确定
1 2 3 4 5 6 | ///回车 确认键 Enter public static void inputEnter(){ // 这里是按下和释放回车键,用于确定是file选项 robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } |
6.1、移动鼠标至指定位置,点击确定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //模拟鼠标左键点击 public static void ClickOnTheLeft(){ //获取屏幕 分辨率 Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize(); int with = new Double(scrSize.getWidth()).intValue(); int height = new Double(scrSize.getHeight()).intValue(); // 移动鼠标到指定屏幕坐标 // robot.mouseMove(with/3+120, height/4+220); //分割标刻鼠标点击位置 (F5) robot.mouseMove(with/3+150, height/4+240); //旋转文本 点击位置 (F5) // 按下鼠标左键 robot.mousePress(InputEvent.BUTTON1_MASK); // 延时100毫秒 robot.delay(100); // 释放鼠标左键(按下后必须要释放, 一次点击操作包含了按下和释放) robot.mouseRelease(InputEvent.BUTTON1_MASK); SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); Date date = new Date(System.currentTimeMillis()); System.out.println("标刻结束时间="+formatter.format(date)); } |
二、总结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | package dlltest; import java.awt.AWTException; import java.awt.Robot; import com.sun.glass.events.KeyEvent; public class RootKeys { public static void main(String[] args) { // TODO Auto-generated method stub try { //初始化 控件 Robot robot = new Robot(); // 组合键 Ctrl + C robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_CONTROL); // 组合键 Ctrl + V robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); //组合键 Alt + L robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_L); robot.keyRelease(KeyEvent.VK_ALT); robot.keyRelease(KeyEvent.VK_L); // Enter\Shift\ Delete \ Home\End\Insert\Esc\BackSpace\Tab robot.keyPress(KeyEvent.VK_TAB); robot.keyPress(KeyEvent.VK_BACKSPACE); robot.keyPress(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_ESCAPE ); robot.keyPress(KeyEvent.VK_SHIFT ); robot.keyPress(KeyEvent.VK_DELETE ); robot.keyPress(KeyEvent.VK_HOME ); robot.keyPress(KeyEvent.VK_END); robot.keyPress(KeyEvent.VK_INSERT ); robot.keyRelease(KeyEvent.VK_INSERT ); robot.keyRelease(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_HOME); robot.keyRelease(KeyEvent.VK_DELETE); robot.keyRelease(KeyEvent.VK_SHIFT); robot.keyRelease(KeyEvent.VK_ESCAPE ); robot.keyRelease(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_BACKSPACE); // F(1\2\3\4\5\6\7\8\9\10\11\12) robot.keyPress(KeyEvent.VK_F1); robot.keyPress(KeyEvent.VK_F2); robot.keyPress(KeyEvent.VK_F3); robot.keyPress(KeyEvent.VK_F4); robot.keyPress(KeyEvent.VK_F5); robot.keyPress(KeyEvent.VK_F6); robot.keyPress(KeyEvent.VK_F7); robot.keyPress(KeyEvent.VK_F8); robot.keyPress(KeyEvent.VK_F9); robot.keyPress(KeyEvent.VK_F10); robot.keyPress(KeyEvent.VK_F11); robot.keyPress(KeyEvent.VK_F12); robot.keyRelease(KeyEvent.VK_F12); robot.keyRelease(KeyEvent.VK_F11); robot.keyRelease(KeyEvent.VK_F10); robot.keyRelease(KeyEvent.VK_F9); robot.keyRelease(KeyEvent.VK_F8); robot.keyRelease(KeyEvent.VK_F7); robot.keyRelease(KeyEvent.VK_F6); robot.keyRelease(KeyEvent.VK_F5); robot.keyRelease(KeyEvent.VK_F4); robot.keyRelease(KeyEvent.VK_F3); robot.keyRelease(KeyEvent.VK_F2); robot.keyRelease(KeyEvent.VK_F1); // 方向键 上下左右 分页 robot.keyPress(KeyEvent.VK_UP); robot.keyPress(KeyEvent.VK_DOWN); robot.keyPress(KeyEvent.VK_LEFT); robot.keyPress(KeyEvent.VK_RIGHT); robot.keyPress(KeyEvent.VK_PAGE_UP); robot.keyPress(KeyEvent.VK_PAGE_DOWN); robot.keyRelease(KeyEvent.VK_PAGE_DOWN); robot.keyRelease(KeyEvent.VK_PAGE_UP); robot.keyRelease(KeyEvent.VK_UP); robot.keyRelease(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_LEFT); robot.keyRelease(KeyEvent.VK_RIGHT); // 小键盘 数字键 0-9 robot.keyPress(KeyEvent.VK_1); robot.keyPress(KeyEvent.VK_2); robot.keyPress(KeyEvent.VK_3); robot.keyPress(KeyEvent.VK_4); robot.keyPress(KeyEvent.VK_5); robot.keyPress(KeyEvent.VK_6); robot.keyPress(KeyEvent.VK_7); robot.keyPress(KeyEvent.VK_8); robot.keyPress(KeyEvent.VK_9); robot.keyPress(KeyEvent.VK_0); robot.keyRelease(KeyEvent.VK_0); robot.keyRelease(KeyEvent.VK_9); robot.keyRelease(KeyEvent.VK_8); robot.keyRelease(KeyEvent.VK_7); robot.keyRelease(KeyEvent.VK_6); robot.keyRelease(KeyEvent.VK_5); robot.keyRelease(KeyEvent.VK_4); robot.keyRelease(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_1); //字母 A - Z robot.keyPress(KeyEvent.VK_A); robot.keyPress(KeyEvent.VK_B); robot.keyPress(KeyEvent.VK_C); //········· robot.keyPress(KeyEvent.VK_X); robot.keyPress(KeyEvent.VK_Y); robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_X); //············ robot.keyRelease(KeyEvent.VK_C); robot.keyRelease(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_A); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } |