Reputation: 25534
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?
Upvotes: 0
Views: 1987
Reputation: 25534
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.
Upvotes: 1
Reputation: 389
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.
Upvotes: 0