java全局键盘_Java 实现全局(全操作系统)的键盘鼠标模拟

Java

实现全局(全操作系统)的键盘鼠标模拟,Java的Robot类实现的是本窗口级的鼠标键盘模拟,Java实现全操作系统级的鼠标和键盘模拟就需要SWT

Win32 Extension来帮助。

(一)键盘模拟

import

org.sf.feeling.swt.win32.extension.Win32;

import

org.sf.feeling.swt.win32.extension.io.Keyboard;

public class KeyboardSnippet {

public static void main(String[] args)

{

try {

Thread.sleep(5000);

} catch (InterruptedException e)

{

e.printStackTrace();

}

Keyboard.keyDown(Win32.VK_LWIN, 0,

false);//键盘上的Win键

Keyboard.keyDown('M', 0,

false);

Keyboard.keyUp('M', 0, false);

Keyboard.keyUp(Win32.VK_LWIN, 0,

false);

}

}

(二)鼠标模拟

import

org.sf.feeling.swt.win32.extension.io.Mouse;

import

org.eclipse.swt.graphics.Point;

import

org.eclipse.swt.widgets.Display;

public class MouseSnippet {

public static void main(String[] args)

{

Mouse.mouseMove(new Point(0, 65535 / 3),

true);

for (int i = 0; i < 1000;

i++) {

Mouse.mouseMove(new Point(1, 0),

false);

try {

Thread.sleep(5);

} catch (InterruptedException e)

{

e.printStackTrace();

}

}

Mouse.mouseClick(Mouse.MOUSE_RIGHT,

Display.getDefault().getCursorLocation(), true);

try {

Thread.sleep(500);

} catch (InterruptedException e)

{

e.printStackTrace();

}

Mouse.mouseMove(new Point(65535 / 2, 65535

/ 2), true);

Mouse.mouseClick(Mouse.MOUSE_LEFT, new

Point(0, 0),false);

for (int i = 0; i < 10;

i++) {

Mouse.mouseWheel(1, true);

try {

Thread.sleep(20);

} catch (InterruptedException e)

{

e.printStackTrace();

}

}

}

}

你可能感兴趣的:(java全局键盘)