Noor
Noor

Reputation: 20150

Cancel a Keypress Event in GWT

I want to cancel a keypress event for a long textbox so that the character newly pressed by the user is not entered in the textbox

longBox_1.addKeyPressHandler(new KeyPressHandler(){
    @Override
    public void onKeyPress(KeyPressEvent event) {
        String Valid="1234567890";
        if (!Valid.contains(String.valueOf(event.getCharCode()))) {
            // the code to cancel the event to be placed here
        }
    }
});

Upvotes: 0

Views: 5237

Answers (1)

Naaooj
Naaooj

Reputation: 910

If your longBox_1 is a private member of your class, or a final var, the code to cancel the event is :

longBox_1.cancelKey();

Otherwise you can cast the source of the event, if you are sure it correspond to the TextBox :

((TextBox)event.getSource()).cancelKey();

Here is the doc for cancelKey :

If a keyboard event is currently being handled on this text box, calling this method will suppress it. This allows listeners to easily filter keyboard input

Upvotes: 3

Related Questions