From d185cfb4dd5b66387d5153ddbec6ebc82cfe4c39 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sun, 11 Nov 2012 18:22:37 -0600 Subject: [PATCH] Added ExtRobot: extensions to Java Robot class. --- project.properties | 6 +- src/main/com/jdbernard/util/ExtRobot.groovy | 71 +++++++++++++++++++++ src/main/com/jdbernard/util/Util.groovy | 29 +++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100755 src/main/com/jdbernard/util/ExtRobot.groovy create mode 100755 src/main/com/jdbernard/util/Util.groovy diff --git a/project.properties b/project.properties index b67f443..ed8527a 100644 --- a/project.properties +++ b/project.properties @@ -1,6 +1,6 @@ -#Mon, 02 Jan 2012 15:07:25 -0600 +#Sun, 11 Nov 2012 18:23:08 -0600 name=jdb-util -version=1.5 +version=1.6 lib.local=true -build.number=2 +build.number=0 diff --git a/src/main/com/jdbernard/util/ExtRobot.groovy b/src/main/com/jdbernard/util/ExtRobot.groovy new file mode 100755 index 0000000..3995757 --- /dev/null +++ b/src/main/com/jdbernard/util/ExtRobot.groovy @@ -0,0 +1,71 @@ +package com.jdbernard.util + +import static com.jdbernard.util.Util.* +import static java.awt.event.KeyEvent.* +import java.awt.Robot + +public class ExtRobot { + + @Delegate Robot robot + + int delayTime + + public ExtRobot() { robot = new Robot() } + + public void pressAndRelease(int keyCode) { + robot.with { + keyPress(keyCode) + keyRelease(keyCode) + delay(delayTime) + } + } + + public void pressCombination(List keyCodes) { + keyCodes.each { keyCode -> robot.keyPress(keyCode) } + robot.delay(delayTime) + keyCodes.reverse().each { keyCode -> robot.keyRelease(keyCode) } + } + + /** + * Currently only tested with alphanumeric! + */ + public void type(String text) { + text.each { letter -> + if (letter.charAt(0).isUpperCase()) + robot.keyPress(VK_SHIFT) + + pressAndRelease((int) letter.toUpperCase().charAt(0)) + + if (letter.charAt(0).isUpperCase()) + robot.keyRelease(VK_SHIFT) + } + } + + public String selectAllAndCopy() { + robot.with { + keyPress(VK_CONTROL) + pressAndRelease(VK_A) + pressAndRelease(VK_C) + keyRelease(VK_CONTROL) + + delay(2 * delayTime) + } + + return readClipboardText() + } + + public void selectAllAndPaste(String text) { + copyToClipboard(text) + + robot.with { + keyPress(VK_CONTROL) + pressAndRelease(VK_A) + pressAndRelease(VK_V) + keyRelease(VK_CONTROL) + + delay(2 * delayTime) + } + + } + +} diff --git a/src/main/com/jdbernard/util/Util.groovy b/src/main/com/jdbernard/util/Util.groovy new file mode 100755 index 0000000..4cea03e --- /dev/null +++ b/src/main/com/jdbernard/util/Util.groovy @@ -0,0 +1,29 @@ +package com.jdbernard.util + +import java.awt.Toolkit +import java.awt.datatransfer.Clipboard +import java.awt.datatransfer.DataFlavor +import java.awt.datatransfer.StringSelection +import java.awt.datatransfer.Transferable + +public class Util { + + static Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard() + + public static String readClipboardText() { + Transferable contents = clipboard.getContents(null) + + if (contents != null && + contents.isDataFlavorSupported(DataFlavor.stringFlavor)) + return (String)contents.getTransferData(DataFlavor.stringFlavor) + + else return null + } + + public static void copyToClipboard(String text) { + StringSelection data = new StringSelection(text) + clipboard.setContents(data, data) + } + + +}