To do this, you'll need to create a class for your button that listens for a Key down. Here's how:
1. First create a class that you can reuse on any form or display you like:
public class ButtonPressListener implements KeyDownHandlerThis class will see a KeyDown event, check that it was the Enter key and then press your button. So if you pass in your default button or OK button, it gets activated when the user presses enter. Here's how:
{
private final Button button;
public ButtonPressListener(final Button button)
{
this.button = button;
}
@Override
public void onKeyDown(final KeyDownEvent key)
{
if (key.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
{
button.click();
}
}
}
First create your button -
private final Button okButton = new Button();Now create your listener, passing the button you want activated when the user hits Enter -
okButton.setText("OK");
final ButtonPressListener listener = new ButtonPressListener(okButton);Now, if your dialog box or form has other input fields, you want each of them to see an Enter key press and activate your OK button. In this example, we have a username and password field on our screen:
usernameTextBox.addKeyDownHandler(listener);
passwordTextBox.addKeyDownHandler(listener);
Now that you know how to setup a key listener, you can create all sorts of conveniences for your user and let them fly through your UI screens.
No comments:
Post a Comment