From 1b33ca6de022539079e43f5271eb844b8ffee48f Mon Sep 17 00:00:00 2001 From: Edward Jakubowski Date: Fri, 21 Mar 2014 01:10:06 -0400 Subject: [PATCH] updates to mouse commands Added setTargetOffset, and mouse will move to target window for click, rightclick and double click --- src/org/synthuse/Api.java | 13 +++++++ src/org/synthuse/CommandPopupMenu.java | 14 +++++--- src/org/synthuse/CommandProcessor.java | 50 +++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/org/synthuse/Api.java b/src/org/synthuse/Api.java index 574b0dd..7c1803e 100644 --- a/src/org/synthuse/Api.java +++ b/src/org/synthuse/Api.java @@ -130,6 +130,8 @@ public class Api { boolean IsWindowVisible(HWND hWnd); int GetWindowRect(HWND hWnd, RECT r); + int MapWindowPoints(HWND hWndFrom, HWND hWndTo, RECT r, int cPoints); + HWND GetDesktopWindow(); HDC GetWindowDC(HWND hWnd); int ReleaseDC(HWND hWnd, HDC hDC); boolean InvalidateRect(HWND hWnd, long lpRect, boolean bErase); @@ -339,6 +341,17 @@ public class Api { user32.SendMessageA(handle, WM_KEYUP, keyCode, null); } + public Point getWindowPosition(HWND handle) { + Point windowPoint = new Point(); + RECT rect = new RECT(); + user32.GetWindowRect(handle, rect); + //System.out.println("rect: l" + rect.left + ",t" + rect.top + ",r" + rect.right + ",b" + rect.bottom); + //user32.MapWindowPoints(user32.GetDesktopWindow(), user32.GetParent(handle), rect, 2); + windowPoint.x = ((rect.right - rect.left) / 2) + rect.left; + windowPoint.y = ((rect.bottom - rect.top) / 2) + rect.top; + return windowPoint; + } + public int getDiskUsedPercentage() { return getDiskUsedPercentage(null); } diff --git a/src/org/synthuse/CommandPopupMenu.java b/src/org/synthuse/CommandPopupMenu.java index e8db402..ae9fd2f 100644 --- a/src/org/synthuse/CommandPopupMenu.java +++ b/src/org/synthuse/CommandPopupMenu.java @@ -56,13 +56,13 @@ public class CommandPopupMenu extends JPopupMenu { JMenu mnMouse = new JMenu("Mouse"); add(mnMouse); - CommandMenuItem mntmClick = new CommandMenuItem("click", 1, false); + CommandMenuItem mntmClick = new CommandMenuItem("click", 2, true); mnMouse.add(mntmClick); - CommandMenuItem mntmDoubleclick = new CommandMenuItem("doubleClick", 1, false); + CommandMenuItem mntmDoubleclick = new CommandMenuItem("doubleClick", 2, true); mnMouse.add(mntmDoubleclick); - CommandMenuItem mntmRightclick = new CommandMenuItem("rightClick", 1, false); + CommandMenuItem mntmRightclick = new CommandMenuItem("rightClick", 2, true); mnMouse.add(mntmRightclick); CommandMenuItem mntmwinClick = new CommandMenuItem("winClick", 2); @@ -89,8 +89,14 @@ public class CommandPopupMenu extends JPopupMenu { CommandMenuItem mntmMouseupright = new CommandMenuItem("mouseUpRight", 1, false); mnMouse.add(mntmMouseupright); - CommandMenuItem mntmMousemove = new CommandMenuItem("mouseMove", 3, false); + CommandMenuItem mntmMousemove = new CommandMenuItem("mouseMove", 2, true); mnMouse.add(mntmMousemove); + + CommandMenuItem mntmMousemoveXy = new CommandMenuItem("mouseMoveXy", 3, false); + mnMouse.add(mntmMousemoveXy); + + CommandMenuItem mntmSetTargetOffset = new CommandMenuItem("setTargetOffset", 3, false); + mnMouse.add(mntmSetTargetOffset); JMenu mnWinMessages = new JMenu("Win Messages"); add(mnWinMessages); diff --git a/src/org/synthuse/CommandProcessor.java b/src/org/synthuse/CommandProcessor.java index 474d41d..8efa3db 100644 --- a/src/org/synthuse/CommandProcessor.java +++ b/src/org/synthuse/CommandProcessor.java @@ -7,6 +7,7 @@ package org.synthuse; +import java.awt.Point; import java.io.PrintWriter; import java.io.StringWriter; import java.sql.Timestamp; @@ -31,6 +32,7 @@ public class CommandProcessor implements Runnable{ public String lastError = ""; public String scriptStr = ""; private Api api = new Api(); + private Point targetOffset = new Point(); private int executeErrorCount = 0; private String currentCommandText = ""; @@ -165,8 +167,12 @@ public class CommandProcessor implements Runnable{ return cmdMouseDownRight(args); if (command.equals("mouseUpRight")) return cmdMouseUpRight(args); - if (command.equals("mouseMove")) + if (command.equals("mouseMove")) return cmdMouseMove(args); + if (command.equals("mouseMoveXy")) + return cmdMouseMoveXy(args); + if (command.equals("setTargetOffset")) + return cmdSetTargetOffset(args); //Windows Api Commands if (command.equals("windowFocus")) @@ -334,16 +340,37 @@ public class CommandProcessor implements Runnable{ } private boolean cmdClick(String[] args) { + if (!checkArgumentLength(args, 1)) + return false; + HWND handle = findHandleWithXpath(args[0]); + if (handle == null) + return false; + Point p = api.getWindowPosition(handle); + RobotMacro.mouseMove(p.x + targetOffset.x, p.y + targetOffset.y); RobotMacro.leftClickMouse(); return true; } private boolean cmdDoubleClick(String[] args) { + if (!checkArgumentLength(args, 1)) + return false; + HWND handle = findHandleWithXpath(args[0]); + if (handle == null) + return false; + Point p = api.getWindowPosition(handle); + RobotMacro.mouseMove(p.x + targetOffset.x, p.y + targetOffset.y); RobotMacro.doubleClickMouse(); return true; } private boolean cmdRightClick(String[] args) { + if (!checkArgumentLength(args, 1)) + return false; + HWND handle = findHandleWithXpath(args[0]); + if (handle == null) + return false; + Point p = api.getWindowPosition(handle); + RobotMacro.mouseMove(p.x + targetOffset.x, p.y + targetOffset.y); RobotMacro.rightClickMouse(); return true; } @@ -369,6 +396,27 @@ public class CommandProcessor implements Runnable{ } private boolean cmdMouseMove(String[] args) { + if (!checkArgumentLength(args, 1)) + return false; + HWND handle = findHandleWithXpath(args[0]); + if (handle == null) + return false; + Point p = api.getWindowPosition(handle); + RobotMacro.mouseMove(p.x + targetOffset.x, p.y + targetOffset.y); + //System.out.println("point " + p.x + "," + p.y); + return true; + } + + private boolean cmdSetTargetOffset(String[] args) { + if (!checkArgumentLength(args, 2)) + return false; + int x = Integer.parseInt(args[0]); + int y = Integer.parseInt(args[1]); + targetOffset.x = x; + targetOffset.y = y; + return true; + } + private boolean cmdMouseMoveXy(String[] args) { if (!checkArgumentLength(args, 2)) return false; int x = Integer.parseInt(args[0]);