开发者

Java input method for Virtual Keyboard

I am facing a 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 a 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.


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();
    }
}


How I sovled it:

//on startup: override the SystemEventQueue
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
final OwnEventQueue newEventQueue = new OwnEventQueue();
eventQueue.push(newEventQueue);
//because dispatchEvent is protected
public class OwnEventQueue {
    private final static OwnEventQueue instance;

    static{
        instance = new OwnEventQueue();
    }

    @Override
    public void dispatchEvent(AWTEvent event) {
        super.dispatchEvent(event);
    }

    public static OwnEventQueue getInstance() {
        return instance;
    }
}
//then onpress of keyboard button
Character character = getCharacter();
int[] events = {KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, KeyEvent.KEY_TYPED};
for (int i = 0; i < events.length; i++) {
    KeyEvent pressKeyEvent = new KeyEvent(focusComponent, events[i], System.currentTimeMillis(), 0, 0, character.charValue());
    OwnEventQueue.getInstance().dispatchEvent(pressKeyEvent);
}
robotKeystrokeSender.keyPress(KeyEvent.VK_RIGHT);
robotKeystrokeSender.delay(10);
robotKeystrokeSender.keyRelease(KeyEvent.VK_RIGHT);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜