简体   繁体   English

传递给onkeyup和onkeydown处理程序的事件未在带有ReactJS的Chrome上设置event.key值

[英]Event passed to onkeyup and onkeydown handlers doesn't set event.key value on Chrome with ReactJS

When i run following code in Chrome I cannot access the event.key value: 当我在Chrome中运行以下代码时,无法访问event.key值:

var Hello = React.createClass({
  getInitialState: function() {
     return { value: 'Empty' }
  },

  keyHandler: function(e){
    e.persist();
    var value = e.type + ':' + e.key;

    this.setState(function(previousState, currentProps) {
       return { value: previousState.value + '\n' + value };
    });
  },

  render: function() {
    return (
    <div>
      <input 
        onKeyDown={this.keyHandler}
        onKeyUp={this.keyHandler}
        onKeyPress={this.keyHandler}
      />
      <pre>{this.state.value}</pre>
     </div>
    );
  }
});

ReactDOM.render(
  <Hello />,
  document.getElementById('container')
);

On latest IE and Firefox it works fine, but on chrome I got following result: 在最新的IE和Firefox上,它可以正常工作,但在chrome上,我得到以下结果: 产生镀铬

As far as I understand React documentation: https://facebook.github.io/react/docs/events.html React's events should provide cross-browser behavior so I'd expect the same result in all browsers. 据我了解React文档: https: //facebook.github.io/react/docs/events.html React的事件应提供跨浏览器的行为,因此我希望在所有浏览器中都可以得到相同的结果。 Did I miss something? 我错过了什么?

PS. PS。 I've created a jsfiddle to play with this issue: https://jsfiddle.net/69z2wepo/38132/ 我创建了一个jsfiddle来解决此问题: https ://jsfiddle.net/69z2wepo/38132/

In the source code you can see that when the key is not on the native event object, React only translates keyCode into the missing key for the special keys: 源代码中你可以看到,当钥匙不在本地事件对象上,阵营只转换键代码转化为特殊键缺少的关键

/**
 * Translation from legacy `keyCode` to HTML5 `key`
 * Only special keys supported, all others depend on keyboard layout or browser
 * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM