Pooya Karimian

Blog Archives: Keyevents in Firefox

« Fusemail | Main | Coding for fun »

Keyevents in Firefox

FarsiI posted about the new PersianJS code in the Persian Computing mailing list. What I've done is to change the code to work in Firefox/Mozilla browsers besides IE, this means allowing Linux/Mac users to use it too.

For catching keypress events and changing them to another key with javascript in Internet Explorer, there's a variable named window.event.keyCode that can both be used both for reading the pressed key and also for changing it to another key.

In Mozilla/Firefox the key code is in a variable named event.which which is defined only in the context of event handler.

The following examples only work in Firefox/Mozilla. The first one shows how to read the key value:

<input type="text" size=20 onkeypress="alert(event.which);">
And the next one is an example of changing the keypress to another key:
<script type="text/javascript">
 function newKey(event) {
     if (event.which==0x23) return;
     var new_event=document.createEvent("KeyEvents");
     new_event.initKeyEvent("keypress", false, true, document.defaultView, false, false, false, false, 0, 0x23);
     event.preventDefault();
     event.target.dispatchEvent(new_event);
 }
</script>
<form action=""><input type="text" size="20" onkeypress="newKey(event)"></form>

Using a simple method for detecting browser type, such as testing for the existance of window.event object, it is possible to write multibrowser codes. PersianJS is a complete example.

Update Aug 5, 2005: Seems like because of a vulnerability initKeyEvent is disabled from Firefox 1.0.6 as you can see in bug #289940. I'm too busy but I will try to write a complete description and comment on that bug hoping that this feature return back to Firefox.

Update March 1, 2007: It's been a while that the bug is fixed. But the syntax for initKeyEvent is changed. You can no longer change the current event but instead you can create a new event and cancel the old one. I fixed the above code to reflect that.



Posted to Programming by pooya at March 10, 2005 09:49 AM
Comments

Posted by: Payam at March 10, 2005 10:26 PM

Great to you Pooya.
You are still creativea and well-organized in development.


Posted by: Pooya at March 10, 2005 10:46 PM

Thanks, it's all the matter of laziness the night before course deadline which forces me to do some (other) work!


Posted by: Merdan at April 19, 2005 04:18 AM

Hi!

Firefox only partially implements the e.which function. If you would like to process the key event that occurs when users navigate with the arrow-keys, then you must use e.keyCode in FireFox. An easy workaround is:

var key;
if(e.which != 0) key = e.which;
else key = e.keyCode

Merdan.


Posted by: eric at September 22, 2006 05:10 PM

.

I was looking for that since a while ! thanks a lot.

.


Posted by: Julie at October 12, 2006 12:05 PM

thankyou so much for this - i scoured the web but this was the simplest workable solution.


Posted by: jenny at February 16, 2007 07:51 AM

Thank you so much.. Merdan

var key;
if(e.which != 0) key = e.which;
else key = e.keyCode

It works.....!


Posted by: Benoît de CHATEAUVIEUX at April 30, 2008 01:38 PM

Many Thanks.
It was the info I was looking for. ;)

Benoît




[Wednesday 2019-10-23] [Updated Saturday 2013-11-30]