How to detect escape key press in IE, Firefox and Chrome?
Below code works in IE and alerts 27
, but in Firefox it alerts 0
$('body').keypress(function(e){
alert(e.which);
if(e.which == 27){
// Close my modal window
}
});
How to detect escape key press in IE, Firefox and Chrome?
Below code works in IE and alerts 27
, but in Firefox it alerts 0
$('body').keypress(function(e){
alert(e.which);
if(e.which == 27){
// Close my modal window
}
});
Note: keyCode
is becoming deprecated, use key
instead.
function keyPress (e) {
if(e.key === "Escape") {
// write your logic here.
}
}
Code Snippet:
var msg = document.getElementById('state-msg');
document.body.addEventListener('keydown', function(e) {
if (e.key == "Escape") {
msg.textContent += 'Escape pressed:'
}
});
Press ESC key <span id="state-msg"></span>
keyCode
and keypress are deprecated.
It seems
keydown
andkeyup
work.
$(document).keyup(function(e) {
if (e.key === "Escape") { // escape key maps to keycode `27`
// <DO YOUR WORK HERE>
}
});
$(document).on('keyup.unique_name', ...)
and $(document).unbind('keyup.unique_name')
–
Fungiform ===
. –
Tamaratamarack ===
over ==
here. –
Unseal .unbind
is now deprecated as of jQuery 3.0... you should use .off
–
Waldenburg 69.0.3497.100 (Official Build) (64-bit) 21st Sep, 18
keydown is working –
Wanton keypress
does not work for Escape
. Use keydown
(or keyup
) instead. –
Einkorn The keydown
event will work fine for Escape and has the benefit of allowing you to use keyCode
in all browsers. Also, you need to attach the listener to document
rather than the body.
Update May 2016
keyCode
is now in the process of being deprecated and most modern browsers offer the key
property now, although you'll still need a fallback for decent browser support for now (at time of writing the current releases of Chrome and Safari don't support it).
Update September 2018
evt.key
is now supported by all modern browsers.
document.onkeydown = function(evt) {
evt = evt || window.event;
var isEscape = false;
if ("key" in evt) {
isEscape = (evt.key === "Escape" || evt.key === "Esc");
} else {
isEscape = (evt.keyCode === 27);
}
if (isEscape) {
alert("Escape");
}
};
Click me then press the Escape key
<input>
element and it will therefore only be triggered when this element has focus. –
Jenine if (evt.key === 'Escape') {
instead of using the deprecated keyCode
–
Foti .key
, see caniuse.com/#feat=keyboardevent-key –
Chancroid Using JavaScript you can do check working jsfiddle
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 27) {
alert('Esc key pressed.');
}
};
Using jQuery you can do check working jsfiddle
jQuery(document).on('keyup',function(evt) {
if (evt.keyCode == 27) {
alert('Esc key pressed.');
}
});
you can attach a listener to keyUp
event for the document.
Also, if you want to make sure, any other key is not pressed along with Esc
key, you can use values of ctrlKey
, altKey
, and shifkey
.
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
//if esc key was not pressed in combination with ctrl or alt or shift
const isNotCombinedKey = !(event.ctrlKey || event.altKey || event.shiftKey);
if (isNotCombinedKey) {
console.log('Escape key was pressed with out any group keys')
}
}
});
check for keyCode
&& which
& keyup
|| keydown
$(document).keydown(function(e){
var code = e.keyCode || e.which;
alert(code);
});
pure JS (no JQuery)
document.addEventListener('keydown', function(e) {
if(e.keyCode == 27){
//add your code here
}
});
keyCode
is deprecated –
Kinakinabalu Below is the code that not only disables the ESC key but also checks the condition where it is pressed and depending on the situation, it will do the action or not.
In this example,
e.preventDefault();
will disable the ESC key-press action.
You may do anything like to hide a div with this:
document.getElementById('myDivId').style.display = 'none';
Where the ESC key pressed is also taken into consideration:
(e.target.nodeName=='BODY')
You may remove this if condition part if you like to apply to this to all. Or you may target INPUT here to only apply this action when the cursor is in input box.
window.addEventListener('keydown', function(e){
if((e.key=='Escape'||e.key=='Esc'||e.keyCode==27) && (e.target.nodeName=='BODY')){
e.preventDefault();
return false;
}
}, true);
Best way is to make function for this
FUNCTION:
$.fn.escape = function (callback) {
return this.each(function () {
$(document).on("keydown", this, function (e) {
var keycode = ((typeof e.keyCode !='undefined' && e.keyCode) ? e.keyCode : e.which);
if (keycode === 27) {
callback.call(this, e);
};
});
});
};
EXAMPLE:
$("#my-div").escape(function () {
alert('Escape!');
})
keypress
doesn't fire on the escape key. At least, not in Chrome on a Mac. –
Skricki keydown
fix the problem. –
Hypoblast i think the simplest way is vanilla javascript:
document.onkeyup = function(event) {
if (event.keyCode === 27){
//do something here
}
}
Updated: Changed key => keyCode
Escape
as event.key
and not 27 –
Brusquerie event.keyCode
to be 27. –
Crossly On Firefox 78 use this ("keypress" doesn't work for Escape key):
function keyPress (e)(){
if (e.key == "Escape"){
//do something here
}
document.addEventListener("keyup", keyPress);
Simple javascript code to detect Escape key is pressed
document.addEventListener("keydown",function(e){
if(e.key === "Escape") {
console.log("Escape key pressed")
}
});
© 2022 - 2024 — McMap. All rights reserved.
keyup
orkeydown
in combination withkeyCode
works in all browsers. – Funds$(document).on("keyup", function (e) {var code = e.keyCode || e.which; alert('key pressed: ' + code);});
Greetings from the 2014 – Luteolin