虚拟键盘的Java输入法

发布于 2024-08-24 10:17:01 字数 729 浏览 8 评论 0原文

我在实现虚拟键盘输入法时遇到问题。目前我正在使用机器人类从虚拟键盘将输入发送到任何应用程序。但为此,我需要创建键码和 unicode 的映射,这在不同的键盘布局上不一致,我可以使用输入法直接将 UNICODE 传递给任何应用程序,而不必担心键码和 unicode 之间的映射吗?

任何有用的链接或示例代码都会有用。

它是一个简单的 Java 程序,始终位于任何应用程序之上,并充当屏幕键盘。使用鼠标同时按下键盘上的任意按钮(键),相应的字符将在下面运行的应用程序中键入。这对于英文字母来说非常有效。我在处理 unicode 时遇到问题。


找到下面的代码片段

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2024-08-31 10:17:07

您的虚拟键盘是否被操作系统用作设备?
或者,换句话说,您是否尝试过将其视为“真正的”键盘?
根据 Java 硬件抽象,如果您的虚拟键盘被视为驱动程序,它应该像真实键盘一样工作。

编辑:根据评论,这不是一个虚拟设备,而是一个Java应用程序,因此,问题是不同的。

根据 Javadoc,Robot 可以发送 键击以 int 形式给出。要从字符创建这些击键,我建议您使用 getKeyStroke(char) 之前使用 getKeycode()。这样,您将拥有与您的 unicode 字符关联的整数值,无论它们是什么。

编辑2:再次修改;-)

看起来像getKeyStroke(String) “应该”处理 unicode 字符。

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.

掀纱窥君容 2024-08-31 10:17:06

我是如何解决的:

//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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文