如何在 TextField 中以编程方式按下一个键? [英] How to press a key programmatically in a TextField?

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

问题描述

我有一个 TextField,我想以编程方式按下切换字符集按钮.我试过了,但我得到了一个 NullPointerException.

I have a TextField, and I want to press the switch charset button programmatically. I've tried this, but I get a NullPointerException.

   myTextfield.addListener(new FocusListener(){
    public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused){
        if(focused){
           InputEvent ie = new InputEvent();
           ie.setKeyCode(Keys.SWITCH_CHARSET);
           actor.fire(ie);
        }
    }
   });

这是日志猫错误

推荐答案

您需要设置事件的类型.这就是导致 NullPointerException 的原因,如果您查看代码就可以看到(LibGDX 既不是封闭源代码,也不是黑魔法).根据您实际想要做什么,您必须选择另一种类型的活动,而不是我选择的活动:

You need to set the type of the event. That's what causes the NullPointerException, which you can see if you look at the code (LibGDX is neither closed source, nor black magic). Depending on what you actually want to do, you have to choose another type of event, than the one I chose:

InputEvent ie = new InputEvent();
ie.setKeyCode(Keys.SWITCH_CHARSET);
ie.setType(Type.keyUp);
actor.fire(ie);

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

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