in partnership with mediatemple

Ticket #111 (new defect)

Opened 1 year ago

Last modified 2 days ago

bug with event.code and event.key in firefox,ie,konqueror(khtml)

Reported by: urielkatz Owned by: kamicane
Type: defect Priority: trivial
Milestone: Mootools version 1.3 Component: Core
Keywords: Cc:

Description (last modified by kamicane) (diff)

in firefox the Event have two properties keyCode and charCode,when pressing on delete keyCode is 46 and charCode is 0,when pressing the dot the charCode is 46 and keyCode is 0,but in both cases event.which is 46 because as far as i understand which is basically (event.keyCode event.charCode) now in both cases the the key in mootools event will be delete,which is of course wrong since there is no distinction if the key is a char or special key. the problematic line in Event.js is(as of v1): this.code = this.event.which this.event.keyCode; using event.which is not ok,we need to use the special keys hash when the code come from keyCode and use String.fromCharCode when the code come from charCode,now this is for firefox,in IE and KHTML(from a bit of testing),pressing the dot will give you the key "delete" and the delete button wouldn't give the key delete.

i think this also happen for the other keys near the delete(like page up/down,end,insert,home).

UPDATE:from some testing in konqueror i see that when pressing the dot event.keyCode,event.charCode and event.which are all 46 and when pressing the delete event.keyCode is 127 and event.charCode is 0.

as i understand there is not standard for key events in w3c dom,there is alot problems with compatibility in browsers.

Change History

Changed 1 year ago by urielkatz

some other buttons that doesn't work:
home will give key $
page down will give "
page up will give !
insert will give -
end will give #

all this in firefox

Changed 1 year ago by anonymous

taken from mochikit,this is how browsers handle keys.

If you're looking for a special key, look for it in keydown or
keyup, but never keypress. If you're looking for a Unicode
chracter, look for it with keypress, but never keyup or
keydown.

Notes:

FF key event behavior:
key event charCode keyCode
DOWN ku,kd 0 40
DOWN kp 0 40
ESC ku,kd 0 27
ESC kp 0 27
a ku,kd 0 65
a kp 97 0
shift+a ku,kd 0 65
shift+a kp 65 0
1 ku,kd 0 49
1 kp 49 0
shift+1 ku,kd 0 0
shift+1 kp 33 0

IE key event behavior:
(IE doesn't fire keypress events for special keys.)
key event keyCode
DOWN ku,kd 40
DOWN kp undefined
ESC ku,kd 27
ESC kp 27
a ku,kd 65
a kp 97
shift+a ku,kd 65
shift+a kp 65
1 ku,kd 49
1 kp 49
shift+1 ku,kd 49
shift+1 kp 33

Safari key event behavior:
(Safari sets charCode and keyCode to something crazy for
special keys.)
key event charCode keyCode
DOWN ku,kd 63233 40
DOWN kp 63233 63233
ESC ku,kd 27 27
ESC kp 27 27
a ku,kd 97 65
a kp 97 97
shift+a ku,kd 65 65
shift+a kp 65 65
1 ku,kd 49 49
1 kp 49 49
shift+1 ku,kd 33 49
shift+1 kp 33 33

Changed 1 year ago by digitarald

  • status changed from new to closed
  • resolution set to invalid

Use Element.addEvent('keydown', fn) and it will give the expected and correct key! If you have a test case which proves that wrong, reopen the ticket with a link to the page.

Changed 1 year ago by anonymous

acctualy in firefox it goes like this

when pressing on delete event.keyCode gives 46, event.charCode gives 0 and event.which also gives 0, when pressing the dot the charCode gives 46, the which gives 46 and keyCode gives 0.

Changed 11 months ago by jesse.mandel@…

  • status changed from closed to reopened
  • resolution deleted

There is still an issue. Say if you want to add a key filter for a textbox to only allow numbers but still allow a few special keys. Adding the following function as the keypress event should only allow you to type numbers and still use backspace, del, tab, left and right:

function filterkeys(myEvent)
{
     var myEvent = new Event(myEvent);

if(myEvent.key=='backspace' || myEvent.key=='tab' || myEvent.key=='left' ||
        myEvent.key=='right' || myEvent.key=='delete' || 
             (myEvent.code >= 48 && myEvent.key <= 57))
{
    return;
}
    
//Cancel input
if (window.event) 
    myEvent.returnValue = false;
else 
    myEvent.preventDefault(); 	
}

Due to the bug mentioned in the original ticket, %, ' and . can be typed because they share the same codes and left, right and del (tab and backspace are control ASCII control chars so they aren't an issue).

Here is a good site about this: http://www.quirksmode.org/js/keys.html

-Jesse

Changed 11 months ago by jesse.mandel@…

UPDATE: I changed my JS code to use addEvent instead of onkeypress and now it doesn't allow ' or . but % still comes through...

Changed 8 months ago by kamicane

  • owner set to kamicane
  • status changed from reopened to new

Changed 8 months ago by kamicane

  • milestone set to Mootools version 1.2

Changed 7 months ago by anonymous

  • component set to Core

More information on this
http://unixpapa.com/js/key.html

Changed 6 months ago by cp3zer0@…

Why not...

el.onkeyup = function (e) {

    var key = e.charCode || e.keyCode;

    /* whatever */

}

...?

Changed 3 months ago by kamicane

  • priority changed from critical to trivial
  • description modified (diff)

Changed 3 months ago by kamicane

  • description modified (diff)
  • milestone changed from Mootools version 1.2 to Mootools version 1.3

Changed 2 days ago by tomocchino

  • milestone set to Mootools version 1.3
Note: See TracTickets for help on using tickets.