How to change the value of e.which in Netscape and FireFox?

Need technical advice on coding your web pages? Covers HTML, JavaScript, CSS, and some server side technologies. Also the issue of some webpages not displayed well.

Moderator: Don_HH2K

How to change the value of e.which in Netscape and FireFox?

Postby shahram » Tue 01 Feb, 2005 5:17 am

Hello all,
For some reason,I need to convert characters typed by user in text fields INSTANTLY. This can be achieved simply in IE by assigning the new value to the "event.KeyCode" . But, in Netscape (and also FireFox and Opera) the similar property "e.which" is readonly.
I searched many sites and newsgroups whithous success. PLease let me know if it is possible at all to do this. Currently I am concating the new character at the end of field which is not a true solution. Because, if the user changes the cursor position either by arrowkeys or mouse click to insert characters at start or middle of field, it still appended to the end of it.

Regards,
Shahram Alemzadeh
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
shahram
new member
new member
 
Posts: 3
Joined: Tue 01 Feb, 2005 4:32 am

Postby akbash » Tue 01 Feb, 2005 2:44 pm

Quite right, all the interesting properties of the event are read-only in Mozilla browsers. So in a word, no, it won't be possible to change the event as it goes by. I don't know exactly what you have in mind of course but I'd be surprised if you couldn't accomplish the same thing with about four lines of Mozilla-specific DOM code: stop the original event before the input field sees it, and dispatch a new one to take its place.

Event processing is an area where you'll see some of the greatest disparity between browsers. The key event is probably the worst: as far as I know there's still no useful DOM spec covering key events. You may have to employ different solutions for every browser. Sorry.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050125 Firefox/1.0+
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby shahram » Thu 03 Feb, 2005 1:32 pm

akbash wrote:I'd be surprised if you couldn't accomplish the same thing with about four lines of Mozilla-specific DOM code: stop the original event before the input field sees it, and dispatch a new one to take its place.


Thank you very much for your reply. I just stuck in writing these 4 lines of code :? :roll: . Whould you please give me a hint on this?

Thanks in advance,
UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
shahram
new member
new member
 
Posts: 3
Joined: Tue 01 Feb, 2005 4:32 am

Postby akbash » Sun 06 Feb, 2005 5:18 pm

I'm curious why this question is suddenly springing up everywhere. But I couldn't find an example already posted, and it's no kind of secret. So:
Code: Select all
  // transmogrify a user-entered Q into an F (Gecko browsers)
  if (String.fromCharCode(event.charCode) == "Q") {
    var newEvent = document.createEvent("KeyEvents")
    newEvent.initKeyEvent("keypress", true, true, document.defaultView,
                          event.ctrlKey, event.altKey, event.shiftKey,
                          event.metaKey, 0, "F".charCodeAt(0))
    event.preventDefault()
    event.target.dispatchEvent(newEvent)
  }

Properly applied, that will alter a Q typed by the user in a text field into an F. It should work in any modern browser (notably not IE), except for the highly variable initKeyEvent. I believe Safari/KDE use the (different) DOM 3 working draft spec from September 2000, and as far as I can tell, at this time Opera has no support for key events at all.
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050125 Firefox/1.0+
akbash
silver member
silver member
 
Posts: 364
Joined: Mon 09 Feb, 2004 9:13 pm

Postby shahram » Sun 06 Feb, 2005 11:27 pm

Thanks a lot Dear Akbash,
Your code works perfectly :D :D . It was kind of you. Thangs again for your help.
Regards,
Shahram
UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
shahram
new member
new member
 
Posts: 3
Joined: Tue 01 Feb, 2005 4:32 am


Return to Web Design and Page Coding

Who is online

Registered users: Google [Bot]