Problem with Event observe and keyCode on IE

20 views
Skip to first unread message

John

unread,
Jun 7, 2007, 6:05:48 AM6/7/07
to Ruby on Rails: Spinoffs
Hello,

I have a problem with the Event.observe method on IE.
When I use The Event.observe method, the key code that I retrieve from
the event,
is different if I use an inline function.

Here is the html code to test it (You must have prototype.js on the
same directory)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Example
</title>
<script src="prototype.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

//browser detection

function maskKeyPress(objEvent) {
var iKeyCode, strKey;

var strUserAgent = navigator.userAgent.toLowerCase();
var isIE = strUserAgent.indexOf("msie") > -1;
if (isIE) {
iKeyCode = objEvent.keyCode;
} else {
iKeyCode = objEvent.which;
}
strKey = String.fromCharCode(iKeyCode);
alert("KeyCode = " + iKeyCode + "\nCharacter =" + strKey);
}

window.onload = function ()
{
var field = document.forms[0].name1;
Event.observe(field, "keypress", maskKeyPress, true );
}
</script>
<style type="text/css">
body, p, td { font-family: Verdana, Arial, Helvetica; font-size:
9pt; }
</style>
</head>
<body>
<h1>Example</h1>
<p>For this example, try typing one of these characters ($,*,%,...)
into the 2 textboxes below. You will see an alert that displays the
character and the keycode corresponding to the key you pressed.
And the keyCode is different when using Event.observe</p>
<form name="tempForm" action="#">
<p><input type="text" name="name1" size="20"></p>
<p><input type="text" name="name2"
onkeypress="maskKeyPress(window.event);" size="20"></p>
</form>
</body>
</html>

Have someone a idea of how to make it work?...
Thank you in advance.

RobG

unread,
Jun 7, 2007, 8:41:34 AM6/7/07
to Ruby on Rails: Spinoffs

On Jun 7, 8:05 pm, John <ichae...@gmail.com> wrote:
> Hello,
>
> I have a problem with the Event.observe method on IE.
> When I use The Event.observe method, the key code that I retrieve from
> the event,
> is different if I use an inline function.
>
> Here is the html code to test it (You must have prototype.js on the
> same directory)

Dunno about the IE issue since I don't have it available right now,
however...

[...]


> //browser detection
>
> function maskKeyPress(objEvent) {
> var iKeyCode, strKey;
>
> var strUserAgent = navigator.userAgent.toLowerCase();
> var isIE = strUserAgent.indexOf("msie") > -1;
> if (isIE) {
> iKeyCode = objEvent.keyCode;
> } else {
> iKeyCode = objEvent.which;
> }

Browser detection is a really bad idea, instead consider:

var iKeyCode = objEvent.keyCode || objEvent.which;


--
Rob

Ryan Gahl

unread,
Jun 7, 2007, 9:51:34 AM6/7/07
to rubyonrail...@googlegroups.com
...and as for the problem you're having. Yep, 'tis a very annoying problem. Can you clarify, though, when the keycode is different? Is it always different, or only for capital letters? Because my problem with this was related to getting the same keycode for a letter, regardless of typing a capital letter or not. They want you to check the modifiers to see if the shift key was also depressed and deduce on your own whether or not it should be capital (which of course doesn't take in to account whether or not the capslock key is on). Anyway... the answer, unfortunately, is to not use Event.observe, but attach the handler directly like this (thus bypass the new W3C event model)...

field.onkeypress = maskKeyPress;

The above is the same result as attaching the handler inline.

This is certainly not an ideal thing to do, but, it does work.

On 6/7/07, RobG <rg...@iinet.net.au> wrote:



On Jun 7, 8:05 pm, John <ichae...@gmail.com> wrote:
> Hello,
>
> I have a problem with the Event.observe method on IE.
> When I use The Event.observe method, the key code that I retrieve from
> the event,
> is different if I use an inline function.
>
> Here is the html code to test it (You must have prototype.js on the
> same directory)

Dunno about the IE issue since I don't have it available right now,
however...

[...]
>                         //browser detection
>
>                         function maskKeyPress(objEvent) {
>                                 var iKeyCode, strKey;
>
>                                 var strUserAgent = navigator.userAgent.toLowerCase();
>                                 var isIE = strUserAgent.indexOf ("msie") > -1;

>                                 if (isIE) {
>                                     iKeyCode = objEvent.keyCode;
>                                 } else {
>                                     iKeyCode = objEvent.which;
>                                 }

Browser detection is a really bad idea, instead consider:

  var iKeyCode = objEvent.keyCode || objEvent.which;


--
Rob



http://www.nthpenguin.com
--
Software Architect
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-262-951-6727
Blog: http://www.someElement.com

John

unread,
Jun 7, 2007, 11:09:00 AM6/7/07
to Ruby on Rails: Spinoffs
Hi,

Thank you for the browser detection tips, Rob ;-)

Ryan, thanks for your help.

In fact, I was using a french keyboard (You would probably ask why.
It's simply because I am one ;-)
And in the french keyboard, there are more characters and the non
standard ASCII characters are not well
managed by the Event.connect method.

Just for information, I have tested with dojo (dojo.event.connect),
just to try and it works....

Thank you for your comments.
Bye....

On Jun 7, 3:51 pm, "Ryan Gahl" <ryan.g...@gmail.com> wrote:
> ...and as for the problem you're having. Yep, 'tis a very annoying problem.
> Can you clarify, though, when the keycode is different? Is it always
> different, or only for capital letters? Because my problem with this was
> related to getting the same keycode for a letter, regardless of typing a
> capital letter or not. They want you to check the modifiers to see if the
> shift key was also depressed and deduce on your own whether or not it should
> be capital (which of course doesn't take in to account whether or not the
> capslock key is on). Anyway... the answer, unfortunately, is to not use
> Event.observe, but attach the handler directly like this (thus bypass the
> new W3C event model)...
>
> field.onkeypress = maskKeyPress;
>
> The above is the same result as attaching the handler inline.
>
> This is certainly not an ideal thing to do, but, it does work.
>

> On 6/7/07, RobG <r...@iinet.net.au> wrote:
>
>
>
>
>
> > On Jun 7, 8:05 pm, John <ichae...@gmail.com> wrote:
> > > Hello,
>
> > > I have a problem with the Event.observe method on IE.
> > > When I use The Event.observe method, the key code that I retrieve from
> > > the event,
> > > is different if I use an inline function.
>
> > > Here is the html code to test it (You must have prototype.js on the
> > > same directory)
>
> > Dunno about the IE issue since I don't have it available right now,
> > however...
>
> > [...]
> > > //browser detection
>
> > > function maskKeyPress(objEvent) {
> > > var iKeyCode, strKey;
>
> > > var strUserAgent =
> > navigator.userAgent.toLowerCase();

> > > var isIE = strUserAgent.indexOf("msie")


> > > -1;
> > > if (isIE) {
> > > iKeyCode = objEvent.keyCode;
> > > } else {
> > > iKeyCode = objEvent.which;
> > > }
>
> > Browser detection is a really bad idea, instead consider:
>
> > var iKeyCode = objEvent.keyCode || objEvent.which;
>
> > --
> > Rob
>

> --
> Ryan Gahl
> Principal, Manager
> Nth Penguin, LLC - Consultinghttp://www.nthpenguin.com

Reply all
Reply to author
Forward
0 new messages