Return to Snippet

Revision: 42309
at March 3, 2011 03:09 by laurenceosx


Updated Code
#!usr/bin/env groovy
//@+leo-ver=4-thin
//@+node:lpt.20110228174854.1369:@thin XRobot.groovy
//@@first
//@@language java
//@@lineending platform
//@+doc
// 
// 3/2/2011
// 
// file: XRobot.groovy
// 
// Laurence Toenjes
// 
// Created with Leo 4.6.3 final, build 2286, September 4, 2009
//     http://webpages.charter.net/edreamleo/front.html
// 
// I created this to help make groovysh act like the python -i option,
// because I got tired of the extra typing of simulating the python 
// interpreter -i option .
// 
// How to use:
// youros> groovysh
// 
// import XRobot
// robot = XRobot.createRobot()
// robot.sendString( '''// How now brown cow.''' )
// 
//@-doc
//@@code
//@+others
//@+node:lpt.20110228174854.1370:imports
import java.awt.Robot;
import java.awt.event.KeyEvent as ke;
import java.awt.event.KeyEvent
//@-node:lpt.20110228174854.1370:imports
//@+node:lpt.20110228174854.1373:class XRobot
class XRobot {
    //@    @+others
    //@+node:lpt.20110228174854.1372:static def createRobot()
    static def createRobot() {
        //@    @+others
        //@+node:lpt.20110228174854.1374:c
        //
        def result = new Robot() {
            //
            //@    @+others
            //@+node:lpt.20110228174854.1377:*** sendString
            def void sendString( aString ) {

                def keyList = []
                aString.each { letter ->
                    def ltr = letter as Character
                    def keyInfoMap = keyCodeForLetter2( ltr )
                    keyList << keyInfoMap // has keys keyCode as int and shift as boolean
                }

                keyList.each { keyInfoItem ->
                    if ( keyInfoItem.shift ) {
                        this.keyPress( KeyEvent.VK_SHIFT )
                    }

                    this.keyCodeClick( keyInfoItem.keyCode as int )

                    if ( keyInfoItem.shift ) {
                        this.keyRelease( KeyEvent.VK_SHIFT )
                    }
                }

            } // end sendString
            //@-node:lpt.20110228174854.1377:*** sendString
            //@+node:lpt.20110228174854.1376:keyCodeClick

            def keyCodeClick( code ) {
                this.keyPress( code as int )
                this.keyRelease( code as int )
            }

            //@-node:lpt.20110228174854.1376:keyCodeClick
            //@+node:lpt.20110228174854.1375:keyCodeForLetter2
            def keyCodeForLetter2( aLetter ) {
                // based on http://gruimed.blogspot.com/2009/09/using-java-robot-to-type-text-strings.html
                //
                // returns a map with keys keyCode as int and shift as boolean
                char c = aLetter as char

                boolean shift = true;
                int keyCode;

                switch (c) {
                    case '~':
                        keyCode = (int)'`';
                        break;

                    case '!':
                        keyCode = (int)'1';
                        break;
                    case '@':
                        keyCode = (int)'2';
                        break;
                    case '#':
                        keyCode = (int)'3';
                        break;
                    case '$':
                        keyCode = (int)'4';
                        break;
                    case '%':
                        keyCode = (int)'5';
                        break;
                    case '^':
                        keyCode = (int)'6';
                        break;
                    case '&':
                        keyCode = (int)'7';
                        break;
                    case '*':
                        keyCode = (int)'8';
                        break;
                    case '(':
                        keyCode = (int)'9';
                        break;
                    case ')':
                        keyCode = (int)'0';
                        break;

                    case ':':
                        keyCode = (int)';';
                        break;
                    case '_':
                        keyCode = (int)'-';
                        break;

                    case '+':
                        keyCode = (int)'=';
                        break;

                    case '|':
                        keyCode = (int)'\\';
                        break;

                    // lpt mod
                    // to handle some problem key strokes
                    // special BEG

                    case '"':
                        keyCode = KeyEvent.VK_QUOTE as int
                        shift   = true
                        break;

                    case '\'':
                        keyCode = KeyEvent.VK_QUOTE as int
                        shift   = false
                        break;

                    case '\n':
                        keyCode = KeyEvent.VK_ENTER as int
                        shift = false;
                    break;

                     // special END

                    case '?':
                        keyCode = (int)'/';
                        break;
                    case '{':
                        keyCode = (int)'[';
                        break;
                    case '}':
                        keyCode = (int)']';
                        break;
                    case '<':
                        keyCode = (int)',';
                        break;
                    case '>':
                        keyCode = (int)'.';
                    break;

                    default:
                        keyCode = (int) c;
                        shift   = false;

                        // lpt mod
                        // handle letters and case
                        def ch = aLetter as Character
                        if ( ch.isLetter() ) {
                            keyCode = (int) ch.toUpperCase();
                            shift = ch.isUpperCase();
                        }


                }
                //
                return ['keyCode': keyCode as int, 'shift': shift] as Map
            }
            //@-node:lpt.20110228174854.1375:keyCodeForLetter2
            //@-others
            //
        } // end new Robot anon class

        result.setAutoWaitForIdle(true)

        return result
        //
        //@-node:lpt.20110228174854.1374:c
        //@-others
    } // end createRobot
    //@nonl
    //@-node:lpt.20110228174854.1372:static def createRobot()
    //@+node:lpt.20110228174854.1378:static def test1

    static def test1() {
        def robot = XRobot.createRobot()
        def testString = '''\

    // How 
    // now
    // brown cow.

    ''';
        robot.sendString( testString )
        return robot
    }
    //@nonl
    //@-node:lpt.20110228174854.1378:static def test1
    //@-others
} // end XRobot
//@-node:lpt.20110228174854.1373:class XRobot
//@-others
//@-node:lpt.20110228174854.1369:@thin XRobot.groovy
//@-leo

Revision: 42308
at March 3, 2011 03:01 by laurenceosx


Initial Code
#!usr/bin/env groovy
//@+leo-ver=4-thin
//@+node:lpt.20110228174854.1369:@thin XRobot.groovy
//@@first
//@@language java
//@@lineending platform
//@+doc
// 
// 3/2/2011
// 
// file: XRobot.groovy
// 
// Laurence Toenjes
// 
// Created with Leo 4.6.3 final, build 2286, September 4, 2009
//     http://webpages.charter.net/edreamleo/front.html
// 
// I created this to help make groovysh act like the python -i option,
// because I got tired of the extra typing of simulating the python 
// interpreter -i option .
// 
// How to use:
// youros> groovysh
// 
// import XRobot
// robot = XRobot.createRobot()
// robot.sendString( '''// How now brown cow.''' )
// 
//@-doc
//@@code
//@+others
//@+node:lpt.20110228174854.1370:imports
import java.awt.Robot;
import java.awt.event.KeyEvent as ke;
import java.awt.event.KeyEvent
//@-node:lpt.20110228174854.1370:imports
//@+node:lpt.20110228174854.1373:class XRobot
class XRobot {
    //@    @+others
    //@+node:lpt.20110228174854.1372:static def createRobot()
    static def createRobot() {
        //@    @+others
        //@+node:lpt.20110228174854.1374:c
        //
        def result = new Robot() {
            //
            //@    @+others
            //@+node:lpt.20110228174854.1377:*** sendString
            def sendString( aString ) {

                def keyList = []
                aString.each { letter ->
                    def ltr = letter as Character
                    def keyInfoMap = keyCodeForLetter2( ltr )
                    keyList << keyInfoMap // has keys keyCode as int and shift as boolean
                }

                keyList.each { keyInfoItem ->
                    if ( keyInfoItem.shift ) {
                        this.keyPress( KeyEvent.VK_SHIFT )
                    }

                    this.keyCodeClick( keyInfoItem.keyCode as int )

                    if ( keyInfoItem.shift ) {
                        this.keyRelease( KeyEvent.VK_SHIFT )
                    }
                }

            } // end sendString
            //@-node:lpt.20110228174854.1377:*** sendString
            //@+node:lpt.20110228174854.1376:keyCodeClick

            def keyCodeClick( code ) {
                this.keyPress( code as int )
                this.keyRelease( code as int )
            }

            //@-node:lpt.20110228174854.1376:keyCodeClick
            //@+node:lpt.20110228174854.1375:keyCodeForLetter2
            def keyCodeForLetter2( aLetter ) {
                // based on http://gruimed.blogspot.com/2009/09/using-java-robot-to-type-text-strings.html
                //
                // returns a map with keys keyCode as int and shift as boolean
                char c = aLetter as char

                boolean shift = true;
                int keyCode;

                switch (c) {
                    case '~':
                        keyCode = (int)'`';
                        break;

                    case '!':
                        keyCode = (int)'1';
                        break;
                    case '@':
                        keyCode = (int)'2';
                        break;
                    case '#':
                        keyCode = (int)'3';
                        break;
                    case '$':
                        keyCode = (int)'4';
                        break;
                    case '%':
                        keyCode = (int)'5';
                        break;
                    case '^':
                        keyCode = (int)'6';
                        break;
                    case '&':
                        keyCode = (int)'7';
                        break;
                    case '*':
                        keyCode = (int)'8';
                        break;
                    case '(':
                        keyCode = (int)'9';
                        break;
                    case ')':
                        keyCode = (int)'0';
                        break;

                    case ':':
                        keyCode = (int)';';
                        break;
                    case '_':
                        keyCode = (int)'-';
                        break;

                    case '+':
                        keyCode = (int)'=';
                        break;

                    case '|':
                        keyCode = (int)'\\';
                        break;

                    // lpt mod
                    // to handle some problem key strokes
                    // special BEG

                    case '"':
                        keyCode = KeyEvent.VK_QUOTE as int
                        shift   = true
                        break;

                    case '\'':
                        keyCode = KeyEvent.VK_QUOTE as int
                        shift   = false
                        break;

                    case '\n':
                        keyCode = KeyEvent.VK_ENTER as int
                        shift = false;
                    break;

                     // special END

                    case '?':
                        keyCode = (int)'/';
                        break;
                    case '{':
                        keyCode = (int)'[';
                        break;
                    case '}':
                        keyCode = (int)']';
                        break;
                    case '<':
                        keyCode = (int)',';
                        break;
                    case '>':
                        keyCode = (int)'.';
                    break;

                    default:
                        keyCode = (int) c;
                        shift   = false;

                        // lpt mod
                        // handle letters and case
                        def ch = aLetter as Character
                        if ( ch.isLetter() ) {
                            keyCode = (int) ch.toUpperCase();
                            shift = ch.isUpperCase();
                        }


                }
                //
                return ['keyCode': keyCode as int, 'shift': shift] as Map
            }
            //@-node:lpt.20110228174854.1375:keyCodeForLetter2
            //@-others
            //
        } // end new Robot anon class

        result.setAutoWaitForIdle(true)

        return result
        //
        //@-node:lpt.20110228174854.1374:c
        //@-others
    } // end createRobot
    //@nonl
    //@-node:lpt.20110228174854.1372:static def createRobot()
    //@+node:lpt.20110228174854.1378:static def test1

    static def test1() {
        def robot = XRobot.createRobot()
        def testString = '''\

    // How 
    // now
    // brown cow.

    ''';
        robot.sendString( testString )
        return robot
    }
    //@nonl
    //@-node:lpt.20110228174854.1378:static def test1
    //@-others
} // end XRobot
//@-node:lpt.20110228174854.1373:class XRobot
//@-others
//@-node:lpt.20110228174854.1369:@thin XRobot.groovy
//@-leo

Initial URL

                                

Initial Description

                                

Initial Title
Groovy Robot SendString

Initial Tags
groovy

Initial Language
Groovy