your way looks ok, using hasClass or check for $(event.target).is(input) or something, where you'd check whatever you want / dont want to be clicked, before running the toggle command.
or hook up the escape button:
$(document).on('keyup',function(e){
if(e.keyCode == 27){
$('#dialog').slideFadeToggle();
}
});