Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 528
Avatar Avatar asked Mar 12 '21 06:03

Avatar


People also ask

How can I tell if Esc key is pressed?

To detect escape key press, keyup or keydown event handler will be used in jquery. It will trigger over the document when the escape key will be pressed from the keyboard. keyup event: This event is fired when key is released from the keyboard. keydown event: This event is fired when key is pressed from the keyboard.

What is escape keycode?

On computer keyboards, the Esc key Esc (named Escape key in the international standard series ISO/IEC 9995) is a key used to generate the escape character (which can be represented as ASCII code 27 in decimal, Unicode U+001B, or Ctrl + [ ).

How do I show or hide the Windows 10 search bar?

You can customize the taskbar to show or hide the Windows 10 Search bar. If the Search box is hidden, this is what the taskbar looks like in Windows 10: To get the Windows 10 Search bar back, right-click or press-and-hold on an empty area on your taskbar to open a contextual menu. Then, access Search and click or tap on "Show search box. "

Why can't I see the search bar on my screen?

Change the screen resolution to show the Windows 10 Search bar Your main display's resolution might be another reason the Search bar is hidden. If you're using a small resolution like 1280x1024 or 800x600, only the Search icon is displayed on your screen.

What is a search bar in HTML?

Search Bar in HTML is a text field for searching the content if we know the keyword in between the actual sentence. Generally, the search box is always added within the navigation bar. We can also add buttons, drop-down lists, and anchors in the navigation bar as per client requirements, along with the search box.

How do I get the escape command on my keyboard?

Caps Lock will now trigger the Escape command! On macOS, you must go to System Preferences > Keyboard, click the Keyboard tab, and then hit the Modifier Keys button. Next, click the popup menu for a modifier key you’d like to change, such as Caps Lock, and assign it a different action like Escape, then click OK.


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').

like image 167
tontonsevilla Avatar answered Oct 24 '22 06:10

tontonsevilla