java win32api 鼠标操作

package TT;

import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

public class UDEUtil {

	
	public static void sendChar(char ch) {
		WinUser.INPUT input = new WinUser.INPUT();
		input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
		input.input.setType("ki"); // Because setting INPUT_INPUT_KEYBOARD is not enough: https://groups.google.com/d/msg/jna-users/NDBGwC1VZbU/cjYCQ1CjBwAJ
		input.input.ki.wScan = new WinDef.WORD(0);
		input.input.ki.time = new WinDef.DWORD(0);
		input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
		// Press
		input.input.ki.wScan = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
		input.input.ki.wVk = new WinDef.WORD(Character.toUpperCase(ch)); // 0x41
		input.input.ki.dwFlags = new WinDef.DWORD(0);  // keydown
		User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());
		// Release
		input.input.ki.dwFlags = new WinDef.DWORD(2);  // keyup
		User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());
	}
	public static void mouse(int x, int y, int botton, boolean abslute) {
		WinUser.INPUT input = new WinUser.INPUT();
		input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_MOUSE);
		input.input.setType("mi");
		if(abslute) {
			x = 65536/User32.INSTANCE.GetSystemMetrics(0)*x;
			y = 65536/User32.INSTANCE.GetSystemMetrics(1)*y;
		}
		if(x!=0)input.input.mi.dx = new WinDef.LONG(x);
		if(y!=0)input.input.mi.dy = new WinDef.LONG(y);
		input.input.mi.dwFlags = new WinDef.DWORD(botton);
		User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值