Cancel a Keypress Event in GWT

孤人 提交于 2019-12-12 05:28:49

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/4872009/cancel-a-keypress-event-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!