How to detect Escape key if search bar of browser is open?

Viewed 556

On my website I provide an editor window (simple HTML div with textarea). It can be closed by Escape key.

Javascript:

$(document).keyup( function(e)
{
    // ESCAPE KEY closes editor window
    if(e.which == 27)
    {
        // trigger cancel button to hide the editor window
        $('#cancelbtn').click();
        return;
    }
});

The problem:

The user might use the browser search bar:

browser search bar

When the user hits the ESC key to close the search, it also triggers the javascript code. Consequently the editor window is closed.

How to detect if the search bar of the browser is open? And then not trigger the ESC key cancel.

1 Answers

I think you need to use keydown instead of keyup to prevent the execution because after the searchbar close it automatically set it focus to the last element it has focus on. Also you you can include this in your conditions to make sure that it will only execute if it is focus on the textarea $('#myTextArea').is(':focus').

Related