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:
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.
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.
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 + [ ).
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. "
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.
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.
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.
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')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With