views:

192

answers:

2

Hi,

I am facing problem in implementing Input method for Virtual Keyboard, currently I am using robot class for sending input to any application from virtual keyboard. but for that I need to create mapping of key-code and unicode, which is not consistent on different keyboard layout, can I directly pass the UNICODE to any application using Input method without worry about mapping between keycode and unicode.

any useful link or sample code will be useful.

It is simple Java program which is always on top of any application and work as onscreen keyboard. using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while I am doing for unicode.

A: 

Is your virtual keyboard used as a device by your OS ? Or, in other words, have you tried considering it as a "real" keyboard ? According to Java hardware abstraction, were your virtual keyboard to be considered as a driver, it should simply work like a real keyboard.

EDIT : according to comment, this is not a virtual device, but a Java application, as a consequence, probleme is different.

According to Javadoc, Robot can send key strokes given as int. To create those key strokes from characters, I would recommand you create them using getKeystroke(char) before to transform them into integer values using getKeycode(). This way, you would have integer values associatesd your unicode characters, whichever they are.

EDIT 2 : once again, a modification ;-)

it seems like getKeyStroke(String) "should" handle unicode characters.

Riduidel
No it is not a device for OS. It is simple Java program which is always on top of any application and work as onscreen keyboard. using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while using I am doing for unicode.
shekhar
Can you edit your question to add these interesting infos ?
Riduidel
I tried what you have suggested but It is not working for UNICODE character. It is always returning "0" as keycode for all the UNICODE character
shekhar
Interesting, can you put an example of the code you use to generate KeyStroke then key code ?
Riduidel
I have posted the code snippet
shekhar
Thank you! but is not working for Devangri unicode characters I don't know why?
shekhar
Well, this time I must confess i'm a bit moved away from my base skills. However, do you display these characters on screen ? If so does it works correctly ?And can you print the keycode used by the awt.robot ? maybe these characters are in the upper range of the unicode table, and as a consequence may be translated as negated integer (resulting in no stroke). But beware : what I say is only a shot in the dark.
Riduidel
Once again thank you Riduide! your input is very useful for me.. I am keep trying.. :)
shekhar
I am able to display the characters on top of the Jbutton and able to display on other application using awt.robot (when I know the KEY_CODE of the UNICODE character). The problem is I am not able to generate the KEY_CODE from the UNICODE charater
shekhar
A: 

Hi, find the code snippet below

   public static void simulateKeyEvent(char key){
    try{
            AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
            int key_code = awtKS.getKeyCode();
            System.out.println("key = "+key+" keyCode = "+key_code);
            robot.keyPress(key_code);
            robot.keyRelease(key_code);

    }catch(Exception e){
            e.printStackTrace();
    }
}
shekhar