RosettaCodeData/Task/Simulate-input-Keyboard/Scala/simulate-input-keyboard.scala

27 lines
755 B
Scala
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
import java.awt.Robot
import java.awt.event.KeyEvent
2015-02-20 00:35:01 -05:00
/** Keystrokes when this function is executed will go to whatever application has focus at the time.
* Special cases may need to be made for certain symbols, but most of
* the VK values in KeyEvent map to the ASCII values of characters.
*/
2013-10-27 22:24:23 +00:00
object Keystrokes extends App {
2015-02-20 00:35:01 -05:00
def keystroke(str: String) {
val robot = new Robot()
for (ch <- str) {
2013-10-27 22:24:23 +00:00
if (Character.isUpperCase(ch)) {
2015-02-20 00:35:01 -05:00
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(ch)
robot.keyRelease(ch)
robot.keyRelease(KeyEvent.VK_SHIFT)
2013-10-27 22:24:23 +00:00
} else {
2015-02-20 00:35:01 -05:00
val upCh = Character.toUpperCase(ch)
robot.keyPress(upCh)
robot.keyRelease(upCh)
2013-10-27 22:24:23 +00:00
}
}
}
2015-02-20 00:35:01 -05:00
keystroke(args(0))
2013-10-27 22:24:23 +00:00
}