导包
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、初始化控件
private static Robot robot;
try {
//初始化 控件
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
2、检测指定窗口是否在运行,并切换至前排
// 切换 指定程序窗口 平面窗口标刻
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、判断窗口是否运行,并模拟按下快捷键运行
/*平面标刻*/
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
//字符串 F2 程序执行键
public static void inputF2(){
// 这里是按下和释放回车键
robot.keyPress(KeyEvent.VK_F2);
robot.keyRelease(KeyEvent.VK_F2);
System.out.println("F2");
}
5、模拟键盘 粘贴内容至可编辑窗口
// 自动录入字符串组合键 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、回车键 确定
///回车 确认键 Enter
public static void inputEnter(){
// 这里是按下和释放回车键,用于确定是file选项
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
6.1、移动鼠标至指定位置,点击确定
//模拟鼠标左键点击
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));
}
二、总结
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();
}
}
}