如何提示用户在焦点上按下后退空间获取警报

时间:2014-04-22 09:35:37

标签: java gwt jsni

我试图提示用户当他从文本字段松开焦点后退空间但是我的代码有小错误它也会提示用户在键入时按下退格键如何发生我不确定请帮助我。

t1.addListener(new TextFieldListenerAdapter() {

       public void onFocus(Field field) {  
     System.out.println("onFocus"); 
       } 
       public void onBlur(Field field) {  
         System.out.println("onBlur");
         call();
            }
    });

    RootPanel.get().add(t1);

        }  

 public native void call()/*-{

 //alert("dfv");

    $wnd.onkeypress = GetChar;

     function GetChar (event)
     {
        var key = event.keyCode;

        if(key==8)//checking keyCode of key for backspace

                {
         var x= window.confirm("Are you sureyou want to leave the page");

        if (x==true)
                 {
              alert(window.history.back())

                  }
           else if(x==false)
         {

          return false;
         }
            }
    }                   

} - * /

1 个答案:

答案 0 :(得分:0)

试试这个

    import com.google.gwt.event.dom.client.KeyCodes;
    import com.google.gwt.i18n.client.DateTimeFormat;
    import com.google.gwt.user.client.Event;
    import com.google.gwt.user.client.Event.NativePreviewEvent;

    ...


    Event.addNativePreviewHandler(new Event.NativePreviewHandler() {

        @Override
        public void onPreviewNativeEvent(final NativePreviewEvent event) {
            if (event.getTypeInt() == Event.ONKEYPRESS) {
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE) {
                    System.out.println("Back space is pressed");
                }
            }
        }
    });

    t1.sinkEvents(Event.ONKEYPRESS);