Добавлено управление курсором, кнопками мыши и клавиатуры. closed #1
This commit is contained in:
parent
329497d6e4
commit
20b1b6370d
@ -5,6 +5,7 @@ import com.annimon.socketfiletransfer.operations.MessageOperation;
|
|||||||
import com.annimon.socketfiletransfer.operations.FileOperation;
|
import com.annimon.socketfiletransfer.operations.FileOperation;
|
||||||
import com.annimon.socketfiletransfer.operations.Operation;
|
import com.annimon.socketfiletransfer.operations.Operation;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Toolkit;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
@ -27,15 +28,18 @@ public class OperationManager extends OperationListener {
|
|||||||
|
|
||||||
public void startCursorControl() throws Exception {
|
public void startCursorControl() throws Exception {
|
||||||
JPanel panel = new JPanel();
|
JPanel panel = new JPanel();
|
||||||
panel.setPreferredSize(new Dimension(100, 100));
|
panel.setFocusable(true);
|
||||||
|
panel.requestFocusInWindow();
|
||||||
|
panel.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
|
||||||
|
|
||||||
JDialog dialog = new JDialog();
|
JDialog dialog = new JDialog();
|
||||||
dialog.add(panel);
|
dialog.add(panel);
|
||||||
|
dialog.setUndecorated(true);
|
||||||
dialog.pack();
|
dialog.pack();
|
||||||
dialog.setVisible(true);
|
dialog.setVisible(true);
|
||||||
|
|
||||||
Operation operation = new CursorOperation(dos);
|
Operation operation = new CursorOperation(dos);
|
||||||
operation.startClientSide(panel);
|
operation.startClientSide(dialog, panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,18 @@ import com.annimon.socketfiletransfer.OperationManager;
|
|||||||
import com.annimon.socketfiletransfer.util.ExceptionHandler;
|
import com.annimon.socketfiletransfer.util.ExceptionHandler;
|
||||||
import com.annimon.socketfiletransfer.util.RobotUtils;
|
import com.annimon.socketfiletransfer.util.RobotUtils;
|
||||||
import java.awt.AWTException;
|
import java.awt.AWTException;
|
||||||
|
import java.awt.Dimension;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.InputEvent;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import javax.swing.JDialog;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,13 +28,16 @@ public class CursorOperation implements Operation {
|
|||||||
NONE = -1,
|
NONE = -1,
|
||||||
TYPE_MOVE = 1,
|
TYPE_MOVE = 1,
|
||||||
TYPE_CLICK = 2,
|
TYPE_CLICK = 2,
|
||||||
|
TYPE_DRAG = 3,
|
||||||
|
TYPE_RELEASED = 4,
|
||||||
|
TYPE_KEY_RELEASED = 5,
|
||||||
STOP = 10;
|
STOP = 10;
|
||||||
|
|
||||||
private DataInputStream dis;
|
private DataInputStream dis;
|
||||||
private DataOutputStream dos;
|
private DataOutputStream dos;
|
||||||
|
|
||||||
private RobotUtils robot;
|
private RobotUtils robot;
|
||||||
private int currentX, currentY, currentMode;
|
private boolean running;
|
||||||
|
|
||||||
private CursorOperation() {
|
private CursorOperation() {
|
||||||
try {
|
try {
|
||||||
@ -51,80 +60,135 @@ public class CursorOperation implements Operation {
|
|||||||
@Override
|
@Override
|
||||||
public void startServerSide() throws IOException {
|
public void startServerSide() throws IOException {
|
||||||
int type = NONE;
|
int type = NONE;
|
||||||
|
// Ñ÷èòûâàåì ðàçìåðû ýêðàíà, ÷òîáû íîðìàëüíî
|
||||||
|
// ïåðåìåùàòü êóðñîð ïðè íåñîâïàäåíèè ðàçðåøåíèé.
|
||||||
|
int width = dis.readInt();
|
||||||
|
int height = dis.readInt();
|
||||||
|
// Ðàñ÷åò ïðèðàùåíèé.
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
double dx = screenSize.getWidth() / (double) width;
|
||||||
|
double dy = screenSize.getHeight() / (double) height;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
type = dis.readInt();
|
type = dis.readInt();
|
||||||
if (type == TYPE_MOVE) {
|
if (type == TYPE_MOVE) {
|
||||||
int dx = dis.readInt();
|
int x = (int) (dis.readInt() * dx);
|
||||||
int dy = dis.readInt();
|
int y = (int) (dis.readInt() * dy);
|
||||||
robot.moveCursor(dx, dy);
|
robot.moveCursor(x, y);
|
||||||
} else if (type == TYPE_CLICK) {
|
} else if (type == TYPE_CLICK) {
|
||||||
System.out.println("Click");
|
int button = dis.readInt();
|
||||||
robot.clickPoint(robot.getCursorPosition());
|
robot.clickPoint(robot.getCursorPosition(), button);
|
||||||
|
} else if (type == TYPE_KEY_RELEASED) {
|
||||||
|
int key = dis.readInt();
|
||||||
|
robot.pressKey(key);
|
||||||
|
} else if (type == TYPE_DRAG) {
|
||||||
|
robot.getRobot().mousePress(InputEvent.BUTTON1_MASK);
|
||||||
|
int x = (int) (dis.readInt() * dx);
|
||||||
|
int y = (int) (dis.readInt() * dy);
|
||||||
|
robot.moveCursor(x, y);
|
||||||
|
} else if (type == TYPE_RELEASED) {
|
||||||
|
robot.getRobot().mouseRelease(InputEvent.BUTTON1_MASK);
|
||||||
}
|
}
|
||||||
} while (type != STOP);
|
} while (type != STOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startClientSide(Object... params) throws Exception {
|
public void startClientSide(Object... params) throws Exception {
|
||||||
JPanel panel = (JPanel) params[0];
|
final JDialog dialog = (JDialog) params[0];
|
||||||
|
JPanel panel = (JPanel) params[1];
|
||||||
|
|
||||||
Point cursor = robot.getCursorPosition();
|
Point cursor = robot.getCursorPosition();
|
||||||
System.out.println("Start tracking cursor. Now: " + cursor.toString());
|
System.out.println("Start tracking cursor. Now: " + cursor.toString());
|
||||||
dos.writeInt(OperationManager.MODE_CURSOR_CONTROL);
|
dos.writeInt(OperationManager.MODE_CURSOR_CONTROL);
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
// Îòïðàâëÿåì ðàçìåð ïàíåëè.
|
||||||
|
dos.writeInt(panel.getPreferredSize().width);
|
||||||
|
dos.writeInt(panel.getPreferredSize().height);
|
||||||
|
|
||||||
MouseTracker tracker = new MouseTracker();
|
MouseTracker tracker = new MouseTracker();
|
||||||
|
KeyTracker keyTracker = new KeyTracker();
|
||||||
panel.addMouseListener(tracker);
|
panel.addMouseListener(tracker);
|
||||||
panel.addMouseMotionListener(tracker);
|
panel.addMouseMotionListener(tracker);
|
||||||
while (tracker.running) {
|
panel.addKeyListener(keyTracker);
|
||||||
|
while (running) {
|
||||||
Thread.sleep(10);
|
Thread.sleep(10);
|
||||||
}
|
}
|
||||||
/*while (true) {
|
dialog.setVisible(false);
|
||||||
dos.writeInt(NONE);
|
System.out.println("Operation stopped");
|
||||||
if (cursor.distance(robot.getCursorPosition()) > 0.5) {
|
|
||||||
Point newPosition = robot.getCursorPosition();
|
|
||||||
int dx = newPosition.x - cursor.x;
|
|
||||||
int dy = newPosition.y - cursor.y;
|
|
||||||
cursor = newPosition;
|
|
||||||
dos.writeInt(TYPE_MOVE);
|
|
||||||
dos.writeInt(dx);
|
|
||||||
dos.writeInt(dy);
|
|
||||||
}
|
|
||||||
if ( (cursor.x == 0) && (cursor.y == 0) ) break;
|
|
||||||
}*/
|
|
||||||
//System.out.println("Operation stopped");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MouseTracker extends MouseAdapter {
|
private class KeyTracker extends KeyAdapter {
|
||||||
|
|
||||||
boolean running = true;
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
if (!running) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
dos.writeInt(TYPE_KEY_RELEASED);
|
||||||
|
dos.writeInt(e.getKeyCode());
|
||||||
|
} catch (IOException ex) { }
|
||||||
|
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MouseTracker extends MouseAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(MouseEvent e) {
|
public void mouseMoved(MouseEvent e) {
|
||||||
|
if (!running) return;
|
||||||
|
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
int y = e.getY();
|
int y = e.getY();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
dos.writeInt(TYPE_MOVE);
|
dos.writeInt(TYPE_MOVE);
|
||||||
dos.writeInt(x - currentX);
|
dos.writeInt(x);
|
||||||
dos.writeInt(y - currentY);
|
dos.writeInt(y);
|
||||||
} catch (IOException ex) { }
|
} catch (IOException ex) { }
|
||||||
|
}
|
||||||
|
|
||||||
currentX = x;
|
@Override
|
||||||
currentY = y;
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
if (!running) return;
|
||||||
|
|
||||||
|
int x = e.getX();
|
||||||
|
int y = e.getY();
|
||||||
|
|
||||||
|
try {
|
||||||
|
dos.writeInt(TYPE_DRAG);
|
||||||
|
dos.writeInt(x);
|
||||||
|
dos.writeInt(y);
|
||||||
|
} catch (IOException ex) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
if (!running) return;
|
||||||
|
try {
|
||||||
|
dos.writeInt(TYPE_RELEASED);
|
||||||
|
} catch (IOException ex) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (!running) return;
|
||||||
try {
|
try {
|
||||||
dos.writeInt(TYPE_CLICK);
|
dos.writeInt(TYPE_CLICK);
|
||||||
dos.writeInt(currentX);
|
dos.writeInt(convertMouseButtonToMask(e.getButton()));
|
||||||
dos.writeInt(currentY);
|
|
||||||
} catch (IOException ex) { }
|
} catch (IOException ex) { }
|
||||||
|
|
||||||
if ( (e.getX() == 0) && (e.getY() == 0) ) {
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int convertMouseButtonToMask(int button) {
|
||||||
|
if (button == MouseEvent.BUTTON1) return MouseEvent.BUTTON1_MASK;
|
||||||
|
if (button == MouseEvent.BUTTON2) return MouseEvent.BUTTON2_MASK;
|
||||||
|
if (button == MouseEvent.BUTTON3) return MouseEvent.BUTTON3_MASK;
|
||||||
|
return button;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import java.awt.PointerInfo;
|
|||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.Robot;
|
import java.awt.Robot;
|
||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
import java.awt.event.InputEvent;
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
@ -24,15 +23,31 @@ public class RobotUtils {
|
|||||||
robot = new Robot();
|
robot = new Robot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Robot getRobot() {
|
||||||
|
return robot;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Êëèêíóòü â íóæíóþ òî÷êó
|
* Êëèêíóòü â íóæíóþ òî÷êó
|
||||||
* @param click òî÷êà ïî êîòîðîé íóæíî êëèêíóòü
|
* @param click òî÷êà ïî êîòîðîé íóæíî êëèêíóòü
|
||||||
|
* @param button íàæàòàÿ êíîïêà ìûøè
|
||||||
*/
|
*/
|
||||||
public void clickPoint(Point click) {
|
public void clickPoint(Point click, int button) {
|
||||||
robot.mouseMove(click.x, click.y);
|
robot.mouseMove(click.x, click.y);
|
||||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
robot.mousePress(button);
|
||||||
robot.delay(CLICK_DELAY);
|
robot.delay(CLICK_DELAY);
|
||||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
robot.mouseRelease(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Íàæàòü êëàâèøó.
|
||||||
|
* @param keyCode êîä êëàâèøè
|
||||||
|
*/
|
||||||
|
public void pressKey(int keyCode) {
|
||||||
|
try {
|
||||||
|
robot.keyPress(keyCode);
|
||||||
|
robot.keyRelease(keyCode);
|
||||||
|
} catch (Exception ex) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,15 +61,14 @@ public class RobotUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Ïåðåäâèíóòü êóðñîð.
|
* Ïåðåäâèíóòü êóðñîð.
|
||||||
* @param dx íà ñêîëüêî ñäâèíóòü ïî-ãîðèçîíòàëè
|
* @param dx êîîðäèíàòà ïî-ãîðèçîíòàëè
|
||||||
* @param dy íà ñêîëüêî ñäâèíóòü ïî-âåðòèêàëè
|
* @param dy êîîðäèíàòà ïî-âåðòèêàëè
|
||||||
*/
|
*/
|
||||||
public void moveCursor(int dx, int dy) {
|
public void moveCursor(int dx, int dy) {
|
||||||
System.out.println(dx + " - " + dy);
|
|
||||||
Point cursorPoint = getCursorPosition();
|
Point cursorPoint = getCursorPosition();
|
||||||
cursorPoint.translate(dx, dy);
|
cursorPoint.translate(dx, dy);
|
||||||
|
|
||||||
robot.mouseMove(cursorPoint.x, cursorPoint.y);
|
robot.mouseMove(dx, dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user