4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JAVA robot を使ったMouse Move

Last updated at Posted at 2015-02-22

マウスを動かしたいと思った時にJavaのRobotがあったのでコードを描いてみた。
外部のファイルから呼び出したい時は
MouseMove hog = new MouseMove();
hog.move(横,縦,カーソルの速さ);で呼び出せばよい。

MouseMove.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class MouseMove {
	public static void main(String[] args) {
		//move(100,100,1);
	}

	static void move(int moW,int moH,int moD){
		Robot r;
		try {
			r = new Robot();
		}
		catch (AWTException e) {
			e.printStackTrace();
			return;
		}
		PointerInfo pI = MouseInfo.getPointerInfo();
		Point point = pI.getLocation();
		
			int i = (int)point.getX();
			int h = (int)point.getY();
		// move
		while (!(i == moW && h == moH)) {
			if (i < moW){
				i++;
			}else if (i > moW) {
				i--;
			}
			if (h < moH){
				h++;
			}else if (h > moH) {
				h--;
			}
			r.mouseMove(i,h);
			r.delay(moD);
		}
	}
}

しかしRobot r;からtry catrhを voidから外に書けないものだろうか…

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?