Skip to content

Instantly share code, notes, and snippets.

@theneosloth
Last active February 16, 2016 02:34
Show Gist options
  • Save theneosloth/a3b7e9314b47ac019a09 to your computer and use it in GitHub Desktop.
Save theneosloth/a3b7e9314b47ac019a09 to your computer and use it in GitHub Desktop.
A simple wrapper for java.awt.Robot
import java.awt.Robot
import java.awt.event.{InputEvent,KeyEvent}
class SimpleBot extends Robot{
val lmb = InputEvent.BUTTON1_MASK
val rmb = InputEvent.BUTTON2_MASK
val min_delay = 200
def leftClick(){
this.mousePress(lmb)
this.delay(min_delay)
this.mouseRelease(rmb)
this.delay(min_delay)
}
def rightClick(){
this.mousePress(rmb)
this.delay(min_delay)
this.mouseRelease(rmb)
this.delay(min_delay)
}
def clickPoint(x: Int, y: Int, delay: Int = 200){
this.mouseMove(x,y)
this.leftClick()
this.delay(delay)
}
def typeKeystroke(code: Int, capital: Boolean = false){
if (capital)
this.keyPress(KeyEvent.VK_SHIFT)
this.keyPress(code)
this.delay(20)
this.keyRelease(code)
if (capital)
this.keyRelease(KeyEvent.VK_SHIFT)
}
def typeChar(charCode: Char){
var keyCode = KeyEvent.getExtendedKeyCodeForChar(charCode);
if (Character.isLetter(charCode))
this.typeKeystroke(keyCode, true)
else
this.typeKeystroke(keyCode, false)
}
def typeString(str: String){
str.foreach(typeChar)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment