I'm using some jQueryUI dialogs like this:
$("#PersonDialog").dialog({
        bgiframe : true,
        autoOpen : false,
        closeOnEscape : true,
        resizable : false,
        height : 150,
        modal : true,
        open: function(){
               activeWindow="dialog"
        },
        close: function(){
               activeWindow="person"
        }
        ....
        ....
    })
I also have some divs that I want to close when escape is pressed. I also use code from this answer, but it didn't solve my problem:
$("body").bindFirst("keydown", function(e) {
        console.info(activeWindow)
            if (e.keyCode == 27) {
                switch(activeWindow){
                    case "dialog":
                         //do something
                    break;
                    case "person":
                        //do something else
                    break;
                    ....
            }
        }
    })
$.fn.bindFirst = function(name, fn) {
    this.on(name, fn);
    this.each(function() {
        var handlers = $._data(this, 'events')[name.split('.')[0]];
        var handler = handlers.pop();
        handlers.splice(0, 0, handler);
    });
};
My problem is that when the dialog is open I press escape, both (div and dialog) get closed and console.info(activeWindow) return "person". I guess that closeOnEscape is fired first and than the keydown-event. Is there a way to change this?