Added ExtRobot: extensions to Java Robot class.
This commit is contained in:
parent
82b0afc490
commit
d185cfb4dd
@ -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
|
||||
|
71
src/main/com/jdbernard/util/ExtRobot.groovy
Executable file
71
src/main/com/jdbernard/util/ExtRobot.groovy
Executable file
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
29
src/main/com/jdbernard/util/Util.groovy
Executable file
29
src/main/com/jdbernard/util/Util.groovy
Executable file
@ -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)
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user