Java 通过JNA 调用Windows Api - 自动发送微信消息给指定用户

所需依赖:jna-5.4.0.jar 和 jna-platform-5.4.0.jar

下载地址: https://github.com/java-native-access/jna

首先需要将微信置顶层:

WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "微信");
        if (hwnd == null) {
            System.out.println("not running");
        } else {
            User32.INSTANCE.ShowWindow(hwnd, 9); 
            User32.INSTANCE.SetForegroundWindow(hwnd);   // bring to front
        }

使用Robot类 模拟安键输入Ctrl+F和Enter查找指定用户

static Robot robot;
	static {
		try {
			if (robot == null)
				robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

将需要输入的内容复制到裁剪版

public static void copy(String text) {
		Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable tText = new StringSelection(text);
		sysc.setContents(tText, null);
		System.out.println("已复制");
	}

再通过Robot模拟Ctrl+V,粘贴文字内容,调用 sendCtrlWith(‘v’);

public static void sendCtrlWith(char c) {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		robot.keyPress(KeyEvent.VK_CONTROL);
		sendKey(c);
		robot.keyRelease(KeyEvent.VK_CONTROL);
	}

    public static void sendKey(char c) {
		try {
			String KeyName = (c+"").toUpperCase();
			Field f = KeyEvent.class.getDeclaredField("VK_" + KeyName);
			Object o = f.get(null);
			int keyCode = Integer.parseInt(o + "");
			robot.keyPress(keyCode);
			robot.keyRelease(keyCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

转载于:https://blog.csdn.net/csdn_lrx/article/details/96630776

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值