Groovy Robot SendString


/ Published in: Groovy
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!usr/bin/env groovy
  2. //@+leo-ver=4-thin
  3. //@+node:lpt.20110228174854.1369:@thin XRobot.groovy
  4. //@@first
  5. //@@language java
  6. //@@lineending platform
  7. //@+doc
  8. //
  9. // 3/2/2011
  10. //
  11. // file: XRobot.groovy
  12. //
  13. // Laurence Toenjes
  14. //
  15. // Created with Leo 4.6.3 final, build 2286, September 4, 2009
  16. // http://webpages.charter.net/edreamleo/front.html
  17. //
  18. // I created this to help make groovysh act like the python -i option,
  19. // because I got tired of the extra typing of simulating the python
  20. // interpreter -i option .
  21. //
  22. // How to use:
  23. // youros> groovysh
  24. //
  25. // import XRobot
  26. // robot = XRobot.createRobot()
  27. // robot.sendString( '''// How now brown cow.''' )
  28. //
  29. //@-doc
  30. //@@code
  31. //@+others
  32. //@+node:lpt.20110228174854.1370:imports
  33. import java.awt.Robot;
  34. import java.awt.event.KeyEvent as ke;
  35. import java.awt.event.KeyEvent
  36. //@-node:lpt.20110228174854.1370:imports
  37. //@+node:lpt.20110228174854.1373:class XRobot
  38. class XRobot {
  39. //@ @+others
  40. //@+node:lpt.20110228174854.1372:static def createRobot()
  41. static def createRobot() {
  42. //@ @+others
  43. //@+node:lpt.20110228174854.1374:c
  44. //
  45. def result = new Robot() {
  46. //
  47. //@ @+others
  48. //@+node:lpt.20110228174854.1377:*** sendString
  49. def void sendString( aString ) {
  50.  
  51. def keyList = []
  52. aString.each { letter ->
  53. def ltr = letter as Character
  54. def keyInfoMap = keyCodeForLetter2( ltr )
  55. keyList << keyInfoMap // has keys keyCode as int and shift as boolean
  56. }
  57.  
  58. keyList.each { keyInfoItem ->
  59. if ( keyInfoItem.shift ) {
  60. this.keyPress( KeyEvent.VK_SHIFT )
  61. }
  62.  
  63. this.keyCodeClick( keyInfoItem.keyCode as int )
  64.  
  65. if ( keyInfoItem.shift ) {
  66. this.keyRelease( KeyEvent.VK_SHIFT )
  67. }
  68. }
  69.  
  70. } // end sendString
  71. //@-node:lpt.20110228174854.1377:*** sendString
  72. //@+node:lpt.20110228174854.1376:keyCodeClick
  73.  
  74. def keyCodeClick( code ) {
  75. this.keyPress( code as int )
  76. this.keyRelease( code as int )
  77. }
  78.  
  79. //@-node:lpt.20110228174854.1376:keyCodeClick
  80. //@+node:lpt.20110228174854.1375:keyCodeForLetter2
  81. def keyCodeForLetter2( aLetter ) {
  82. // based on http://gruimed.blogspot.com/2009/09/using-java-robot-to-type-text-strings.html
  83. //
  84. // returns a map with keys keyCode as int and shift as boolean
  85. char c = aLetter as char
  86.  
  87. boolean shift = true;
  88. int keyCode;
  89.  
  90. switch (c) {
  91. case '~':
  92. keyCode = (int)'`';
  93.  
  94. case '!':
  95. keyCode = (int)'1';
  96. case '@':
  97. keyCode = (int)'2';
  98. case '#':
  99. keyCode = (int)'3';
  100. case '$':
  101. keyCode = (int)'4';
  102. case '%':
  103. keyCode = (int)'5';
  104. case '^':
  105. keyCode = (int)'6';
  106. case '&':
  107. keyCode = (int)'7';
  108. case '*':
  109. keyCode = (int)'8';
  110. case '(':
  111. keyCode = (int)'9';
  112. case ')':
  113. keyCode = (int)'0';
  114.  
  115. case ':':
  116. keyCode = (int)';';
  117. case '_':
  118. keyCode = (int)'-';
  119.  
  120. case '+':
  121. keyCode = (int)'=';
  122.  
  123. case '|':
  124. keyCode = (int)'\\';
  125.  
  126. // lpt mod
  127. // to handle some problem key strokes
  128. // special BEG
  129.  
  130. case '"':
  131. keyCode = KeyEvent.VK_QUOTE as int
  132. shift = true
  133.  
  134. case '\'':
  135. keyCode = KeyEvent.VK_QUOTE as int
  136. shift = false
  137.  
  138. case '\n':
  139. keyCode = KeyEvent.VK_ENTER as int
  140. shift = false;
  141.  
  142. // special END
  143.  
  144. case '?':
  145. keyCode = (int)'/';
  146. case '{':
  147. keyCode = (int)'[';
  148. case '}':
  149. keyCode = (int)']';
  150. case '<':
  151. keyCode = (int)',';
  152. case '>':
  153. keyCode = (int)'.';
  154.  
  155. keyCode = (int) c;
  156. shift = false;
  157.  
  158. // lpt mod
  159. // handle letters and case
  160. def ch = aLetter as Character
  161. if ( ch.isLetter() ) {
  162. keyCode = (int) ch.toUpperCase();
  163. shift = ch.isUpperCase();
  164. }
  165.  
  166.  
  167. }
  168. //
  169. return ['keyCode': keyCode as int, 'shift': shift] as Map
  170. }
  171. //@-node:lpt.20110228174854.1375:keyCodeForLetter2
  172. //@-others
  173. //
  174. } // end new Robot anon class
  175.  
  176. result.setAutoWaitForIdle(true)
  177.  
  178. return result
  179. //
  180. //@-node:lpt.20110228174854.1374:c
  181. //@-others
  182. } // end createRobot
  183. //@nonl
  184. //@-node:lpt.20110228174854.1372:static def createRobot()
  185. //@+node:lpt.20110228174854.1378:static def test1
  186.  
  187. static def test1() {
  188. def robot = XRobot.createRobot()
  189. def testString = '''\
  190.  
  191. // How
  192. // now
  193. // brown cow.
  194.  
  195. ''';
  196. robot.sendString( testString )
  197. return robot
  198. }
  199. //@nonl
  200. //@-node:lpt.20110228174854.1378:static def test1
  201. //@-others
  202. } // end XRobot
  203. //@-node:lpt.20110228174854.1373:class XRobot
  204. //@-others
  205. //@-node:lpt.20110228174854.1369:@thin XRobot.groovy
  206. //@-leo

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.