java按键处理程序无法处理第一个字母
我需要在每次按键事件中打印方法中的内容。我已经尝试了下面的代码,问题是,第一次按键总是返回null。然而,在键入第二个字母后,它将打印第一个键事件。按键事件不是在第一个事件中捕获字母。你能帮我解决这个问题吗
final StringComboBox searchGridTextBox = new StringComboBox();
searchGridTextBox.setEmptyText("Search Grid");
searchGridTextBox.addFocusHandler(new FocusHandler(){
@Override
public void onFocus(FocusEvent event){
if(searchGridTextBox.getStore().size() > 0)
searchGridTextBox.expand();
}
});
searchGridTextBox.addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
System.out.println("On key press event ") ;
}
});
# 1 楼答案
还有另外两个处理程序可以使用keyUp和keyDown处理程序。尝试使用keyUp/keyDown处理程序,看看它是否满足您的要求
在组合框为空的情况下,按键的行为有所不同,本文对此进行了解释:
https://stackoverflow.com/a/42036960/3612019
# 2 楼答案
对于这种情况,需要使用KeyUpEvent。请在下面找到更新的代码