/    Sign up×
Community /Pin to ProfileBookmark

Keypress event does not seem to be captured by MSIE 5.x on Mac

Hi,

I am trying to capture the keypress event to change the default behaviour when the return key is pressed. However, on MSIE on Macintosh (IE 5.x- at least all the versions I have tried on both OS 9 and OS 10) , when the onkeypress trigger is executed the events charCode and keyCode are both 0. The event type also shows as “focus” instead of “keypress”. the following is snippets of the code I used.

Javascript:

function keyPress(e) {

if (!e) {
var e = window.event;
}

alert(“event keycode is ” + e.keyCode);
alert(“event type is ” + e.type);
alert(“charcode is ” + e.charCode);

key = (e.keyCode!=undefined) ? e.keyCode : key=e.which;

}

HTML:

<BODY onkeypress=”return keyPress(event);”>

Can anyone tell me how I can get around this and capture the key code. The code performs correctly on IE on the PC.

thanks in advance,

Jeff Bowes

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JOct 28.2004 — Have you tried [B]onkeydown[/B]
Copy linkTweet thisAlerts:
@jbowesauthorOct 29.2004 — Yes. Unfortunately, onkeydown produces the same resuts as onkeypress, with the exception that on the OS X version of IE the event is "blur" instead of "focus". The results of onkeyup are the same.
Copy linkTweet thisAlerts:
@Mr_JOct 29.2004 — I am fumbling in the dark here because I do not know anything about Macs, I'm sorry.

I tried your code with my win IE6 and got an empty alert box for

alert("charcode is " + e.charCode);

Mozilla did not return the correct info either


Here's what did work, hope this helps


[SIZE=1<HTML>

<HEAD>

<TITLE>Document Title</TITLE>

<script>

moz=document.getElementById&&!document.all

function keyPress(e) {

pressed_key=(!moz?event.keyCode:e.which)

alert("event keycode is " + pressed_key);

alert("event type is " + (!moz?event.type:e.type))

alert("charcode is " + String.fromCharCode(pressed_key))

//key = (e.keyCode!=undefined) ? e.keyCode : key=e.which;

}

</script>

</HEAD>

<BODY onkeypress="return keyPress(event);">

</BODY>

</HTML>

[/SIZE]
×

Success!

Help @jbowes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...