简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM