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

On 2013/08/11 10:42, Jamie Lokier wrote:
> 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.

Hi, it's unfortunate fact. However, I don't understand why the book 
references .key (and also .char?) now. The .key spec is still discussed. 
Actually, some key names are changed by the latest draft. So, the .key 
is still too unstable for most websites. If web developers need to touch 
it, they need to check all updates on all browsers.

The purposes of .key implementation on Gecko are:

1. Experiments to implement .key on a lot of platforms (IE which was the 
only one browser supporting .key is designed only for Windows).
2. Provides some non-printable key handling chance which are not 
supported on .keyCode.

Actually, we find a lot of additional necessary key names. And we 
improved our product behavior internally.

>      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.

You should not reference .key if you want to keep clean your code since 
.key spec is going to be updated. Additionally, current spec doesn't 
define the printable key's key name in some cases. So, the book's code 
looks really hacky for me (I don't know what's intended by the author, 
though).

# FYI: .char will be dropped from web standards.

-- 
Masayuki Nakano <masayuki@d-toybox.com>
Manager, Internationalization, Mozilla Japan.

Received on Monday, 12 August 2013 03:17:28 UTC