0

I want to generate key events for Special characters like £, €, µ, ½, Ö, Ä etc. I am able to generate keyevents for key which are on my keyboard like 'A,B,c, %, *, ^' etc with following code:

public static void generateKeyEvent(final int c) {

        new Thread() {
            public void run() {

                try {
                    Robot robot = new Robot();
                    robot.keyPress(c);

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }
                    robot.keyRelease(c);
                } catch (Exception e) {
                e.printStackTrace();
                }
            }
        }.start();

    }

In case of normal characters, it is working fine but in case of characters which i mentioned above the code is throwing following exception:

java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.companyname.utils.Abc$1.run(Abc.java:286)

One thing which i noticed during my search for the solution to this problem, as these special characters are not mapped on my keyboard that is why it is throwing this exception.

any idea, how can i do this?

mudit
  • 25,306
  • 32
  • 90
  • 132

2 Answers2

1

I got answer to this problem.. basically if you want to print symbol like those then you need to "alt" key for typing that.

for example: if you need to type 'é' in notepad you have to type alt+130.

So i did the same, i generated the key event for alt then for numpad 1 then numpad3 and finally numpad0.

mudit
  • 25,306
  • 32
  • 90
  • 132
0

How are you passing the keys? Note that Robot.keyPress expects key code, not character. Take a look at the KeyEvent constants. There is a VK_EURO_SIGN, not sure about the others. You should be able to get an arbitrary key code by implementing a KeyListener and checking KeyEvent.getKeyCode() when the particular key (combination of keys) is pressed.

Stefan Buynov
  • 389
  • 1
  • 7
  • I have created a View which has buttons for these special characters and when user presses it, i need to generate the key event. – mudit May 21 '12 at 06:17
  • Can you provide some sample code? When you say View - is this an Android application? – Stefan Buynov May 21 '12 at 11:28
  • why would i be generating AWT events in android? it is a native java application. For view code: it is a simple jframe which has a jbutton. – mudit May 21 '12 at 11:52
  • Well, in that case you simply use `robot.keyPress(KeyEvent.VK_EURO_SIGN)`. For the symbols you don't have a constant for, you can [implement a KeyListener](http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html) and get the keyCode by typing the symbol from the keyboard. – Stefan Buynov May 21 '12 at 13:42
  • that actually doesnt work because you dont have that key on your keyboard and it throws IllegalArgumentException as i mentioned in my question. – mudit May 22 '12 at 11:09