KeyPress handler not working for first letter
I need to print something in a method on every keypress event. I tried the below code and the problem with it is the first key press always returning null. Whereas, after entering the second letter, it prints the first key event. The key event does not capture the letter on the first event. Can you help resolve this?
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 ") ;
}
});
+3
source to share
2 answers
For this scenario, you need to use KeyUpEvent. Please find the updated code below.
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.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
System.out.println("On key up event ") ;
}
});
+1
source to share