From 20b1b6370d67e37ada424fa03cad466d71ee5fc8 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 1 Jun 2013 20:54:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D1=83=D1=80=D1=81=D0=BE=D1=80=D0=BE=D0=BC?= =?UTF-8?q?,=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D0=B0=D0=BC=D0=B8=20=D0=BC?= =?UTF-8?q?=D1=8B=D1=88=D0=B8=20=D0=B8=20=D0=BA=D0=BB=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=B0=D1=82=D1=83=D1=80=D1=8B.=20closed=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socketfiletransfer/OperationManager.java | 8 +- .../operations/CursorOperation.java | 144 +++++++++++++----- .../socketfiletransfer/util/RobotUtils.java | 30 +++- 3 files changed, 132 insertions(+), 50 deletions(-) diff --git a/src/com/annimon/socketfiletransfer/OperationManager.java b/src/com/annimon/socketfiletransfer/OperationManager.java index 45e03d7..65ad051 100644 --- a/src/com/annimon/socketfiletransfer/OperationManager.java +++ b/src/com/annimon/socketfiletransfer/OperationManager.java @@ -5,6 +5,7 @@ import com.annimon.socketfiletransfer.operations.MessageOperation; import com.annimon.socketfiletransfer.operations.FileOperation; import com.annimon.socketfiletransfer.operations.Operation; import java.awt.Dimension; +import java.awt.Toolkit; import java.io.File; import javax.swing.JDialog; import javax.swing.JPanel; @@ -27,15 +28,18 @@ public class OperationManager extends OperationListener { public void startCursorControl() throws Exception { JPanel panel = new JPanel(); - panel.setPreferredSize(new Dimension(100, 100)); + panel.setFocusable(true); + panel.requestFocusInWindow(); + panel.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); JDialog dialog = new JDialog(); dialog.add(panel); + dialog.setUndecorated(true); dialog.pack(); dialog.setVisible(true); Operation operation = new CursorOperation(dos); - operation.startClientSide(panel); + operation.startClientSide(dialog, panel); } } diff --git a/src/com/annimon/socketfiletransfer/operations/CursorOperation.java b/src/com/annimon/socketfiletransfer/operations/CursorOperation.java index 8b29483..dc8830c 100644 --- a/src/com/annimon/socketfiletransfer/operations/CursorOperation.java +++ b/src/com/annimon/socketfiletransfer/operations/CursorOperation.java @@ -4,12 +4,18 @@ import com.annimon.socketfiletransfer.OperationManager; import com.annimon.socketfiletransfer.util.ExceptionHandler; import com.annimon.socketfiletransfer.util.RobotUtils; import java.awt.AWTException; +import java.awt.Dimension; 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.MouseEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import javax.swing.JDialog; import javax.swing.JPanel; /** @@ -22,13 +28,16 @@ public class CursorOperation implements Operation { NONE = -1, TYPE_MOVE = 1, TYPE_CLICK = 2, + TYPE_DRAG = 3, + TYPE_RELEASED = 4, + TYPE_KEY_RELEASED = 5, STOP = 10; private DataInputStream dis; private DataOutputStream dos; private RobotUtils robot; - private int currentX, currentY, currentMode; + private boolean running; private CursorOperation() { try { @@ -51,80 +60,135 @@ public class CursorOperation implements Operation { @Override public void startServerSide() throws IOException { 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 { type = dis.readInt(); if (type == TYPE_MOVE) { - int dx = dis.readInt(); - int dy = dis.readInt(); - robot.moveCursor(dx, dy); - } else if (type == TYPE_CLICK) { - System.out.println("Click"); - robot.clickPoint(robot.getCursorPosition()); + int x = (int) (dis.readInt() * dx); + int y = (int) (dis.readInt() * dy); + robot.moveCursor(x, y); + } else if (type == TYPE_CLICK) { + int button = dis.readInt(); + 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); } @Override 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(); System.out.println("Start tracking cursor. Now: " + cursor.toString()); dos.writeInt(OperationManager.MODE_CURSOR_CONTROL); + running = true; + + // Отправляем размер панели. + dos.writeInt(panel.getPreferredSize().width); + dos.writeInt(panel.getPreferredSize().height); + MouseTracker tracker = new MouseTracker(); + KeyTracker keyTracker = new KeyTracker(); panel.addMouseListener(tracker); panel.addMouseMotionListener(tracker); - while (tracker.running) { + panel.addKeyListener(keyTracker); + while (running) { Thread.sleep(10); } - /*while (true) { - dos.writeInt(NONE); - 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"); + dialog.setVisible(false); + System.out.println("Operation stopped"); } - public class MouseTracker extends MouseAdapter { - - boolean running = true; + private class KeyTracker extends KeyAdapter { + + @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 public void mouseMoved(MouseEvent e) { + if (!running) return; + int x = e.getX(); int y = e.getY(); try { dos.writeInt(TYPE_MOVE); - dos.writeInt(x - currentX); - dos.writeInt(y - currentY); + dos.writeInt(x); + dos.writeInt(y); } catch (IOException ex) { } - - currentX = x; - currentY = y; } @Override - public void mouseClicked(MouseEvent e) { - try { - dos.writeInt(TYPE_CLICK); - dos.writeInt(currentX); - dos.writeInt(currentY); - } catch (IOException ex) { } + public void mouseDragged(MouseEvent e) { + if (!running) return; - if ( (e.getX() == 0) && (e.getY() == 0) ) { - running = false; - } + 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 + public void mouseClicked(MouseEvent e) { + if (!running) return; + try { + dos.writeInt(TYPE_CLICK); + dos.writeInt(convertMouseButtonToMask(e.getButton())); + } catch (IOException ex) { } + } + + 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; + } } } diff --git a/src/com/annimon/socketfiletransfer/util/RobotUtils.java b/src/com/annimon/socketfiletransfer/util/RobotUtils.java index 086d815..1a938be 100644 --- a/src/com/annimon/socketfiletransfer/util/RobotUtils.java +++ b/src/com/annimon/socketfiletransfer/util/RobotUtils.java @@ -7,7 +7,6 @@ import java.awt.PointerInfo; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; -import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; @@ -24,15 +23,31 @@ public class RobotUtils { robot = new Robot(); } + public Robot getRobot() { + return robot; + } + /** * Кликнуть в нужную точку * @param click точка по которой нужно кликнуть + * @param button нажатая кнопка мыши */ - public void clickPoint(Point click) { + public void clickPoint(Point click, int button) { robot.mouseMove(click.x, click.y); - robot.mousePress(InputEvent.BUTTON1_MASK); + robot.mousePress(button); 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 dy на сколько сдвинуть по-вертикали + * @param dx координата по-горизонтали + * @param dy координата по-вертикали */ public void moveCursor(int dx, int dy) { - System.out.println(dx + " - " + dy); Point cursorPoint = getCursorPosition(); cursorPoint.translate(dx, dy); - robot.mouseMove(cursorPoint.x, cursorPoint.y); + robot.mouseMove(dx, dy); } /**