如何以编程方式模拟Java FX中的箭头键按下 [英] How to programmatically simulate arrow key presses in Java FX

查看:168
本文介绍了如何以编程方式模拟Java FX中的箭头键按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的JFX应用程序模拟箭头按键(当它们在TextField中注册时),但我无法弄清楚如何发送除字符串或字节之外的任何内容。

I want to make my JFX application simulate arrow key presses (when they are registered in a TextField), but I can't figure out how to send anything other than Strings or bytes.

我想象的是这样的:

static EventHandler<KeyEvent> KEY() {
    E = new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent ke) {
            if (ke.getCode().equals(KeyCode.UP)) {
                try {
                    //someObject.SimulateKeyPress(KeyCode.UP);

                    //OR

                    //coolObject.SendKey((char)KEY_UPKEY));
                } catch (Exception ex) {
                    //Teleport goats
                }
            }
        }
    };

    return E;
}


推荐答案

使用班级机器人

 try {
    Robot r = new Robot();
    //there are other methods such as positioning mouse and mouseclicks etc.
    r.keyPress(java.awt.event.KeyEvent.VK_UP);
    r.keyRelease(java.awt.event.KeyEvent.VK_UP);
 } catch (AWTException e) {
    //Teleport penguins  
 }

这篇关于如何以编程方式模拟Java FX中的箭头键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆