12

We have a user provided string that may contain unicode characters, and we want the robot to type that string.

How do you convert a string into keyCodes that the robot will use?
How do you do it so it is also java version independant (1.3 -> 1.6)?

What we have working for "ascii" chars is

//char c = nextChar();
//char c = 'a'; // this works, and so does 'A'
char c = 'á'; // this doesn't, and neither does 'Ă'
Robot robot = new Robot();
KeyStroke key = KeyStroke.getKeyStroke("pressed " + Character.toUpperCase(c) );
if( null != key ) {
  // should only have to worry about case with standard characters
  if (Character.isUpperCase(c))
  {
    robot.keyPress(KeyEvent.VK_SHIFT);
  }

  robot.keyPress(key.getKeyCode());
  robot.keyRelease(key.getKeyCode());

  if (Character.isUpperCase(c))
  {
    robot.keyRelease(KeyEvent.VK_SHIFT);
  }
}
Greg Domjan
  • 13,943
  • 6
  • 43
  • 59

4 Answers4

10

Based on javamonkey79's code I've created the following snippet which should work for all Unicode values...

public static void pressUnicode(Robot r, int key_code)
{
    r.keyPress(KeyEvent.VK_ALT);

    for(int i = 3; i >= 0; --i)
    {
        // extracts a single decade of the key-code and adds
        // an offset to get the required VK_NUMPAD key-code
        int numpad_kc = key_code / (int) (Math.pow(10, i)) % 10 + KeyEvent.VK_NUMPAD0;

        r.keyPress(numpad_kc);
        r.keyRelease(numpad_kc);
    }

    r.keyRelease(KeyEvent.VK_ALT);
}

This automatically goes through each decade of the unicode key-code, maps it to the corresponding VK_NUMPAD equivalent and presses/releases the keys accordingly.

  • 2
    No, this solution relies on the Windows-only (AFAIK) method of using Alt+Numpad to enter unicode characters. – Luke H Feb 16 '14 at 21:25
  • @LukeH - for Mac I just found - First go to international system preferences-> input menu and check the box next to "Unicode hex". then switch to unicode input in the menu bar. now to enter a unicode character you hold option(alt) and enter the hexadecimal unicode value. [Apple discussions](https://discussions.apple.com/thread/1899290?tstart=0) – Greg Domjan Aug 08 '14 at 16:05
  • For linux - Hold down 'ctrl' & 'shift' the entire time while entering the character. While holding them down, press 'U' then the 4 digit octal code for the character. https://pthree.org/2006/11/30/its-unicode-baby/ – Greg Domjan Aug 08 '14 at 16:10
  • Beware that AltCodes do not support full unicode set, but only characters in IBM437 (ALT nnn) and Windows-1252 (ALT 0nnn) character sets -- see e.g. [here](https://altcodeunicode.com/). It is possible to enable full unicode support via "ALT" "+" "nnnn" (see e.g. [here](https://superuser.com/a/1024956/534074)). YMMV – vlp Oct 29 '19 at 15:51
3

The KeyEvent Class does not have direct mappings for many unicode classes in JRE 1.5. If you are running this on a Windows box what you may have to do is write a custom handler that does something like this:

Robot robot = new Robot();
char curChar = 'Ã';

// -- isUnicode( char ) should be pretty easy to figure out
if ( isUnicode( curChar ) ) {
   // -- this is an example, exact key combinations will vary
   robot.keyPress( KeyEvent.VK_ALT );

   robot.keyPress( KeyEvent.VK_NUMBER_SIGN );
   robot.keyRelease( KeyEvent.VK_NUMBER_SIGN );

   // -- have to apply some logic to know what sequence
   robot.keyPress( KeyEvent.VK_0 );
   robot.keyRelease( KeyEvent.VK_0 );
   robot.keyPress( KeyEvent.VK_1 );
   robot.keyRelease( KeyEvent.VK_1 );
   robot.keyPress( KeyEvent.VK_9 );
   robot.keyRelease( KeyEvent.VK_9 );
   robot.keyPress( KeyEvent.VK_5 );
   robot.keyRelease( KeyEvent.VK_5 );

   robot.keyRelease( KeyEvent.VK_ALT );
}

e.g. Figure out what they key combinations are, and then map them to some sort of Object (maybe a HashMap?) for later lookup and execution.

Hope this helps :)

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
  • Ouchie, I was hoping for simpler, but this will certainly help. – Greg Domjan Dec 29 '08 at 06:07
  • I've found that you don't need KeyEvent.VK_NUMBER_SIGN. Also that the numbers need to be entered as if by the numpad, ie. KeyEvent.VK_NUMPAD0. – Greg Domjan Dec 30 '08 at 01:05
  • @Greg: I figured you'd have to play with the exact configuration to get it right. Just a suggestion but you can also try converting the char to an int then to a String then a char array such that you can parse through each one ... just a thought. – javamonkey79 Dec 31 '08 at 05:36
0

i think this is a bit late but...

Robot robot = new Robot();

   robot.keyPress( KeyEvent.VK_DEAD_ACUTE);

   robot.keyPress( KeyEvent.VK_A );
   robot.keyRelease( KeyEvent.VK_A );

   robot.keyRelease( KeyEvent.VK_DEAD_ACUTE );

that just type an "á"

hecvd
  • 659
  • 1
  • 8
  • 24
-1

The best way that i find when solve simulare problem

import java.awt.AWTException;
import java.awt.Robot;

public class MyRobot {

    public static void typeString(String s)
        {
            try {
            Robot robik = new Robot();
            byte[] bytes = s.getBytes();
            for (byte b : bytes)
            {
                int code = b;
                // keycode only handles [A-Z] (which is ASCII decimal [65-90])
                if (code > 96 && code < 123) code = code - 32;
                robik.delay(40);
                robik.keyPress(code);
                robik.keyRelease(code);
            }
        } catch (AWTException e){

    }
  }

}

http://www.devdaily.com/java/java-robot-class-example-mouse-keystroke\

Vladimir Bosyi
  • 433
  • 1
  • 5
  • 7
  • Thanks, nice neat layout for dealing with ascii characters, but doesn't cover the issue of unicode which was the main point. – Greg Domjan Jun 21 '12 at 06:01