.mouseleave function also affecting back, forward and refresh buttons

I am extremely new to JavaScript. This is infact the first time I am using it. So I have been trying to redirect the searches from the browser address bar. I was looking for an eventlistener or a jQuery function that would listen to whenever the user hit enter on the address bar, but I have been told that it is not possible. So I was working on a work around, i.e, I am going to target the body and if the search came from the body, I will not redirect it. I used to following code:

$(document).ready(function() {
     $('body').keypress(function (e) {
         var key = e.which;
         if(key == 13)  // the enter key code
         {
            safari.extension.dispatchMessage("DoNotOverride")
         }
     });
     $("body").click(function(){
        safari.extension.dispatchMessage("DoNotOverride")
     });    
}) 

This work perfect in all situations, except one. If the user makes a dummy enter on the body without changing his url, and then searches on the addressbar, the logic fails. So I tried to add the following code:

$("body").mouseleave(function(){
    safari.extension.dispatchMessage("Override")
}

This, however, cause more problems as the back button and refresh buttons were also causing overrides even after i treid multiple fuctions like popstate() to handle it.

Can anybody think of an alternate to .mouseleave function that I can use in this scenario? I don't want to use .mouseleave at all. I am just looking for a way to handle those dummy clicks and enters on the body.

I am very new to JS, so I am certain I am missing a lot of things.