0

I am trying to prompt user when he loose focus from text field on press back space but my code hav little bugs it also promp user when he press backspace during typing how its happening i an not sure please help me.

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
    How is it possible to loose the focus on Backspace? Please provide sample code. – Braj Apr 22 '14 at 09:51
  • I am confused between onfocus and onblur when i click text field it print onfocus and when i click on blur it print on blur. in bulr i have a jsni function which geting the backspace key code and pronpt user ,but it also prompt when i click onfocus ..My all code is above –  Apr 22 '14 at 10:32
  • Are you using GXT? What version? You code is not complete. – Braj Apr 22 '14 at 10:56
  • i am using GWT EXT.version i am not sure.please help how it will work –  Apr 22 '14 at 11:01

1 Answers1

0

Try this one

    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);
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thanks for response but its work for both during textfield typing and on blur –  Apr 22 '14 at 11:22
  • It just a hint. I'll try to provide you a complete solution. Right now I am busy. Start working in this direction. – Braj Apr 22 '14 at 11:24
  • i am using history listener class if there is any use to check history token on back press because the url will we change on back press –  Apr 22 '14 at 12:24
  • this only work for page refresh and closing window . it is not working for back button –  Apr 22 '14 at 17:59
  • I think you are asking at wrong post. your actual post is [how can i get a prompt on url change](http://stackoverflow.com/questions/23225387/how-can-i-get-a-prompt-on-url-change). Am I right :) – Braj Apr 22 '14 at 18:01