How to press a key programmatically in a TextField?

这一生的挚爱 提交于 2019-12-24 11:02:04

问题


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

This is the log cat error


回答1:


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


来源:https://stackoverflow.com/questions/23221188/how-to-press-a-key-programmatically-in-a-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!