dominikh / go-js-dom

MIT License
252 stars 42 forks source link

Add a way to extract what key was pressed to KeyboardEvent. #8

Closed dmitshur closed 10 years ago

dmitshur commented 10 years ago

I need to be able to get extract what key was pressed (looking for Escape, Enter keys).

I've just looked into the various docs [1][2][3], and they seem to suggest that:

The KeyboardEvent was briefly (from 2003-2010) defined to have keyIdentifier and keyLocation attributes, but these were removed in favor of the current key and location attributes. These attributes were not widely implemented.

But when using the latest stable Chrome and Safari, I'm seeing the opposite. key is not present at all, but keyIdentifier is set. With this test page,

<html><body><input onkeydown="console.log(event);"></body></html>

This is what I'm seeing:

image

The only two fields that I see to identify which key was pressed are keyCode and keyIdentifier.

Can you please add one of them? Or if I'm mistaken, suggest how I can determine if Escape or Enter were pressed using existing API.

I think I would prefer keyIdentifier (over keyCode) if it indeed is the current "standard". But I'm not opposed to you adding both.

[1] https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent [2] http://www.w3.org/TR/DOM-Level-3-Events/ [3] http://unixpapa.com/js/key.html

dominikh commented 10 years ago

Closed via 73b8f9a. See the commit message for more information.

dmitshur commented 10 years ago

Confirmed, that works for me. Thanks!