By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

How to figure out if 'Esc' has been pressed

P: n/a
With the following code I can figure out if 'Enter' has been pressed:

... onKeyPress = "if (check_enter(event)) {alert('Enter has been
pressed');}"

function check_enter(eventobjekt) {
var var_key;

if (navigator.appName == \"Microsoft Internet Explorer\") {
var_key = window.event.keyCode;
}
else {
var_key = eventobjekt.which;
}

return (var_key == 13);
}

This works fine in IE and Mozilla.
Now I'd like to write a function which figures out if 'Esc' has been
pressed.
I thought I could just replace
return (var_key == 13);
with
return (var_key == 27);

However this works in IE but not in Mozilla.

How can I figure out if 'Esc' has been pressed in Mozilla?
Stefan
Jan 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Stefan Mueller wrote:
With the following code I can figure out if 'Enter' has been pressed:

... onKeyPress = "if (check_enter(event)) {alert('Enter has been
pressed');}"

function check_enter(eventobjekt) {
var var_key;

if (navigator.appName == \"Microsoft Internet Explorer\") {
var_key = window.event.keyCode;
}
else {
var_key = eventobjekt.which;
}

return (var_key == 13);
}

You should test for features, not for a modifiable string like appName,
e.g.:

function check_enter(eventobjekt) {
var var_key;

if (window.event && window.event.keyCode) {
var_key = window.event.keyCode;
}else if (eventobjekt && eventobjekt.which) {
var_key = eventobjekt.which;
}
return (var_key == 13);
}
This works fine in IE and Mozilla.
Now I'd like to write a function which figures out if 'Esc' has been
pressed.
I thought I could just replace
return (var_key == 13);
with
return (var_key == 27);

However this works in IE but not in Mozilla.

How can I figure out if 'Esc' has been pressed in Mozilla?


function check_esc(eventobjekt) {
var var_key;

if (window.event && window.event.keyCode) {
var_key = window.event.keyCode;
} else if (eventobjekt && eventobjekt.keyCode) {
var_key = eventobjekt.keyCode;
}
if (var_key == 27)
alert('Esc has been pressed');
}

if (document.addEventListener) {
document.addEventListener('keypress', check_esc, false);
} else if (document.attachEvent) {
document.attachEvent('onkeypress', check_esc);
}
JW
Jan 28 '06 #2

P: n/a
> You should test for features, not for a modifiable string like appName,

I'm a beginner and I'm very happy about such comments. Many thanks.
The modified code also works fine.

I tried to rewrite your code as a function because I just like to call a
function like
... onKeyPress = "if (check_esc(event)) {alert('Esc has been pressed');}"

But it still doesn't work in Mozilla and I also don't know how program that
check_esc returns true or false. If possible I would like to have only one
function.
(PS: I've added (eventobject) to check_esc2 - don't I need this or did you
miss it?)

function check_esc(eventobjekt) {
if (document.addEventListener) {
document.addEventListener('keypress', check_esc2(eventobjekt), false);
}
else if (document.attachEvent) {
document.attachEvent('onkeypress', check_esc2(eventobjekt));
}
}

function check_esc2(eventobjekt) {
var var_taste = 0;

if (window.event && window.event.keyCode) {
var_taste = window.event.keyCode;
}
else if (eventobjekt && eventobjekt.which) {
var_taste = eventobjekt.which;
}

if (var_key == 27) {
alert('Esc has been pressed');
}
}

Many thanks
Stefan
Jan 28 '06 #3

P: n/a
Stefan Mueller wrote:
But it still doesn't work in Mozilla and I also don't know how
program that check_esc returns true or false. If possible I would
like to have only one function.
[...] function check_esc2(eventobjekt) {
var var_taste = 0;

Here, for some reason, you are defining "var_taste" instead of "var_key"
else if (eventobjekt && eventobjekt.which) {
var_taste = eventobjekt.which;
}


When you look closely at the code in my previous reply, you will notice that
not "eventobjekt.which" but "eventobjekt.keyCode" is referenced.

Corrected code below, which can be called the way you like it:

function check_esc(eventobjekt) {
var var_key = 0;

if (window.event && window.event.keyCode) {
var_key = window.event.keyCode;
}
else if (eventobjekt && eventobjekt.keyCode) {
var_key = eventobjekt.keyCode;
}

return (var_key == 27);
}
JW
Jan 28 '06 #4

P: n/a
> Here, for some reason, you are defining "var_taste" instead of "var_key"

Sorry for that, I just forgot to translate it ;-)

Your code works perfect!

Many thanks for your help.
Stefan
Jan 29 '06 #5

P: n/a
Janwillem Borleffs wrote on 29 jan 2006 in comp.lang.javascript:
function check_esc(eventobjekt) {
var var_key = 0;

if (window.event && window.event.keyCode) {
var_key = window.event.keyCode;
}
else if (eventobjekt && eventobjekt.keyCode) {
var_key = eventobjekt.keyCode;
}

return (var_key == 27);
}


<script>
function check_esc(e) {
var var_key = e.keyCode || e.wich;
if (var_key && (var_key == 27))
alert ('Escape');
}
</script>

<input type="text" onkeydown="check_esc(event);">

IE6 & FF1.5 tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 29 '06 #6

P: n/a

Stefan Mueller wrote:
Here, for some reason, you are defining "var_taste" instead of "var_key"


Sorry for that, I just forgot to translate it ;-)


That's hilarious! With so many other ways for us to write buggy code,
it is easy to forget that non-English speakers may have to have a
consistent translation policy in place to ensure they are
creating/using consistent function/variable names.

Cheers
Nic

Jan 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.