Re: [D3E] Gecko will start supporting KeyboardEvent.key from Firefox 23, but only for non-printable keys

Masayuki Nakano wrote:
> Hello.
> 
> I have good news, I implemented KeyboardEvent.key *only* for 
> non-printable keys on Gecko.
> https://bugzilla.mozilla.org/show_bug.cgi?id=842927
> The first release of it is Firefox 23 released at early of this August.
...
> For printable keys, Gecko returns "MozPrintableKey". When we support 
> .char attribute, we will fix it. I believe that this decision doesn't 
> make current web developers confused.

Hi,

I'm afraid this did break some reference code, in the big and well
known O'Reilly book "Javascript: The Definitive Guide" by David
Flanagan, 6th edition.

The reference code looks like this (shown simplified without stuff
about modifiers etc. to demonstrate the issue):

    function dispatch(event) {

        if (event.key)
            keyname = event.key;
        else if (event.keyIdentifier && event.keyIdentifier.substring(0,2) !== "U+")
            keyname = event.keyIdentifier;
        else
            keyname = keyCodeToKeyName[event.keyCode];

        if (keyname) {
            var handler = map[keyname];
            if (handler) {
                handler.call(...);
            }
    }

Before Gecko was changed, this was fine.  Now that event.key ===
"MozPrintableKey", the code calculates the wrong value for keyname, so
fails to dispatch handlers for the right keys.

So this decision has unfortunately broken existing code, and means
application logic for handling different browsers key events now needs
another special case to work around Gecko's new behaviour, if it was
trying to do the right thing by looking at event.key first.

(I am not sure if the code should be refusing to use event.key when
event.key === 'Unidentified'.  I'm not sure if, when a browser sets
event.key to that, it implies there's no useful key name anyway.  Or
if the browser's list of keys it has names for is less than the list
of useful keyCodes it reports.)

I can't see what the code should do about the latest Gecko behaviour,
other than making a special case to match "MozPrintableKey", like this:

    if (event.key && event.key !== "MozPrintableKey")
        keyname = event.key;
    else if (event.keyIdentifier && event.keyIdentifier.substring(0,2) !== "U+")
        keyname = event.keyIdentifier;
    else
        keyname = keyCodeToKeyName[event.keyCode];

That looks quite ugly.  Is there a cleaner way of doing this?  Perhaps
I missed something obvious.

I would be surprised if this is the only code affected in this way.

Thanks,
-- Jamie

Received on Sunday, 11 August 2013 01:43:12 UTC